From 8a0bfedd64a791efac7e7fceaabafdcb220d750e Mon Sep 17 00:00:00 2001 From: root Date: Mon, 4 Jun 2018 18:25:58 +0200 Subject: [PATCH] Data fixtures --- Dockerfile | 0 composer.json | 1 + composer.lock | 123 ++++++++++++++++++++++++++++++- config/bundles.php | 1 + src/DataFixtures/.gitignore | 0 src/DataFixtures/AppFixtures.php | 90 ++++++++++++++++++++++ symfony.lock | 12 +++ 7 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 src/DataFixtures/.gitignore create mode 100644 src/DataFixtures/AppFixtures.php diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json index a916f3c..272232c 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "php": "^7.1.3", "ext-iconv": "*", "api-platform/api-pack": "^1.1", + "doctrine/doctrine-fixtures-bundle": "^3.0", "symfony/console": "^4.0", "symfony/flex": "^1.0", "symfony/framework-bundle": "^4.0", diff --git a/composer.lock b/composer.lock index a270e39..366967e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "df1fb0339808419998d06c9ecb09ab6f", + "content-hash": "6fb85b3fa57133c433a33fb80ed9c9c6", "packages": [ { "name": "api-platform/api-pack", @@ -441,6 +441,66 @@ ], "time": "2017-08-31T08:43:38+00:00" }, + { + "name": "doctrine/data-fixtures", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a", + "reference": "3a1e2c3c600e615a2dffe56d4ca0875cc5233e0a", + "shasum": "" + }, + "require": { + "doctrine/common": "~2.2", + "php": "^7.1" + }, + "conflict": { + "doctrine/phpcr-odm": "<1.3.0" + }, + "require-dev": { + "doctrine/dbal": "^2.5.4", + "doctrine/orm": "^2.5.4", + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "For using MongoDB ODM with PHP 7", + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\DataFixtures\\": "lib/Doctrine/Common/DataFixtures" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "database" + ], + "time": "2018-03-20T09:06:36+00:00" + }, { "name": "doctrine/dbal", "version": "v2.7.1", @@ -689,6 +749,67 @@ ], "time": "2018-03-27T09:22:12+00:00" }, + { + "name": "doctrine/doctrine-fixtures-bundle", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/doctrine/DoctrineFixturesBundle.git", + "reference": "7fc29d2b18c61ed99826b21fbfd1ff9969cc2e7f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/7fc29d2b18c61ed99826b21fbfd1ff9969cc2e7f", + "reference": "7fc29d2b18c61ed99826b21fbfd1ff9969cc2e7f", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "~1.0", + "doctrine/doctrine-bundle": "~1.0", + "php": ">=5.5.9|^7.0", + "symfony/doctrine-bridge": "~2.7|~3.0|~4.0", + "symfony/framework-bundle": "^3.3|^4.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^3.3" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Bundle\\FixturesBundle\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Doctrine Project", + "homepage": "http://www.doctrine-project.org" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony DoctrineFixturesBundle", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "Fixture", + "persistence" + ], + "time": "2017-12-04T20:26:38+00:00" + }, { "name": "doctrine/doctrine-migrations-bundle", "version": "v1.3.1", diff --git a/config/bundles.php b/config/bundles.php index da80416..02a81b8 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -12,4 +12,5 @@ return [ Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true], ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true], Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true], + Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/src/DataFixtures/.gitignore b/src/DataFixtures/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php new file mode 100644 index 0000000..6e5b657 --- /dev/null +++ b/src/DataFixtures/AppFixtures.php @@ -0,0 +1,90 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace App\DataFixtures; + +use App\Entity\Question; +use App\Entity\Response; +use Doctrine\Bundle\FixturesBundle\Fixture; +use Doctrine\Common\Persistence\ObjectManager; + +class AppFixtures extends Fixture +{ + + public function __construct() + { + // + } + + public function load(ObjectManager $manager) + { + $this->loadQuestions($manager); + //$this->loadResponses($manager); + } + + private function loadQuestions(ObjectManager $manager) + { + foreach ($this->getData() as $data) { + $question = new Question(); + $question->setText($data['text']); + + foreach ($data['responses'] as $response) { + $newResponse = new Response(); + $newResponse->setText($response['text']); + $newResponse->setQuestion($question->getId()); + $manager->persist($newResponse); + + $question->addResponse($newResponse); + } + + $manager->persist($question); + //$this->addReference($username, $user); + } + + $manager->flush(); + } + + private function loadResponses(ObjectManager $manager) + { + // + } + + private function getData() + { + return [ + [ + "text" => "ma question 1", + "responses" => [ + [ + "text" => "option1", + "child" => 1, + ], + [ + "text" => "option2", + ], + ] + ], + [ + "text" => "ma question 2", + "responses" => [ + [ + "text" => "option1", + ], + [ + "text" => "option2", + ], + [ + "text" => "option3", + ], + ] + ] + ]; + } +} \ No newline at end of file diff --git a/symfony.lock b/symfony.lock index b053bee..3613c7d 100644 --- a/symfony.lock +++ b/symfony.lock @@ -29,6 +29,9 @@ "doctrine/common": { "version": "v2.8.1" }, + "doctrine/data-fixtures": { + "version": "v1.3.1" + }, "doctrine/dbal": { "version": "v2.7.1" }, @@ -44,6 +47,15 @@ "doctrine/doctrine-cache-bundle": { "version": "1.3.3" }, + "doctrine/doctrine-fixtures-bundle": { + "version": "3.0", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "master", + "version": "3.0", + "ref": "2ea6070ecf365f9a801ccaed4b31d4a3b7af5693" + } + }, "doctrine/doctrine-migrations-bundle": { "version": "1.2", "recipe": {