dev-master
9999999-devHelping you work with Doctrine Fixtures much faster
MIT
The Requires
by Theodor Diaconu
Helping you work with Doctrine Fixtures much faster
The main idea of this is to decouple your DataFixture classes to avoid spaghetti, and write elegant code by using a bit of functional PHP., (*1)
This works with both ODM and ORM of Doctrine., (*2)
composer require theodordiaconu/doctrine-fixture-helper dev-master
Add the fixtures bundle to AppKernel.php:, (*3)
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
To understand how it works, let's take the following scenario:, (*4)
In your DataFixtures/ORM folder from your bundle, we recommend having a Configuration class:, (*5)
class Configuration { const USERS = 100; const BLOG_POSTS_PER_USER = 5; const COMMENTS_PER_BLOG_POST = 15; public static $jobTitles = [ 'Manager', 'Customer Relation', 'Web-Consultant', 'Web Architect', 'Loan Provider', 'Boss' ]; }
Let's begin with Users., (*6)
use TD\FixtureHelper\BaseFixture; class LoadUserData extends BaseFixture { public function doLoad() { $this->userService = $this->container->get('user_service'); $this->iterator(Configuration::USERS, function($index) { $user = new User(); $user->setFirstname($this->faker->firstname()); $user->setLastname($this->faker->lastname()); $user->setJob($this->faker->randomElement(Configuration::$jobTitles)); $this->userService->doSomething($user); return $user; // you must return the object. }, 'user'); // note: user is our reference name, the script will create user-1, user-2, user-3 accordingly. } public function getOrder() { return 1; } }
Now let's create some blog posts for each user, (*7)
// in LoadBlogPostData.php public function doLoad() { $this->iterator('user', function(User $user) { // this will iterate through all existing users $this->iterator(Configuration::BLOG_POSTS_PER_USER, function($index) use ($user) { $blog = new BlogPost($user); $blog->setTitle($this->faker->sentence()); $blog->setText($this->faker->text()); return $blog; }, 'post') }); } // update getOrder
Now let's leave comments to the blog posts:, (*8)
// in LoadCommentData.php public function doLoad() { $this->iterator('post', function(BlogPost $post) { $this->iterator(Configuration::COMMENTS_PER_BLOG_POST, function($index) use ($post) { $comment = new Comment($this->getRandomObject('user')); $comment->setPost($post); return $comment; }, 'comment-for-'.$post->getId()) }); } // update getOrder
As you might have already noticed, if the iterator gets an integer as the first parameter, it iterates one by one until that limit is reached, if it gets a string it assumes that you want to iterate through a collection of pre-existing object references., (*9)
As you can see we have written this with very few lines of code. And the sky is the limit. This will help you create very complex and interconnected entities., (*10)
$this->container // container $this->manager // ObjectManager $this->faker // faker $this->getObjects('user') // will return all users $this->getReference('user-1') // will return user-1 $this->getRandomObject('user') // will return a random element from user collection
Take a look at faker helper methods: https://github.com/fzaninotto/Faker, (*11)
Helping you work with Doctrine Fixtures much faster
MIT