Buy

Fixture References & Relating Objects

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Having just one fixture class that loads articles and comments... and eventually other stuff, is not super great for organization:

78 lines src/DataFixtures/ArticleFixtures.php
... lines 1 - 8
class ArticleFixtures extends BaseFixture
{
... lines 11 - 27
public function loadData(ObjectManager $manager)
{
$this->createMany(Article::class, 10, function(Article $article, $count) use ($manager) {
... lines 31 - 61
$comment1 = new Comment();
... lines 63 - 67
$comment2 = new Comment();
... lines 69 - 72
});
... lines 74 - 75
}
}

Let's give the comments their own home. First, delete the comment code from here:

66 lines src/DataFixtures/ArticleFixtures.php
... lines 1 - 8
class ArticleFixtures extends BaseFixture
{
... lines 11 - 27
public function loadData(ObjectManager $manager)
{
$this->createMany(Article::class, 10, function(Article $article, $count) use ($manager) {
... lines 31 - 60
});
... lines 62 - 63
}
}

Then, find your terminal and run:

php bin/console make:fixture

Call it CommentFixture.

Flip back to your editor and open that file!

18 lines src/DataFixtures/CommentFixture.php
... lines 1 - 2
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
class CommentFixture extends Fixture
{
public function load(ObjectManager $manager)
{
// $product = new Product();
// $manager->persist($product);
$manager->flush();
}
}

In the last tutorial, we made a cool base class with some extra shortcuts. Extend BaseFixture:

17 lines src/DataFixtures/CommentFixture.php
... lines 1 - 6
class CommentFixture extends BaseFixture
{
... lines 9 - 15
}

Then, instead of load, we now need loadData(), and it should be protected. Remove the extra use statement on top:

17 lines src/DataFixtures/CommentFixture.php
... lines 1 - 4
use Doctrine\Common\Persistence\ObjectManager;
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
... lines 11 - 14
}
}

Thanks to our custom base class, we can create a bunch of comments easily with $this->createMany(), passing it Comment::class, 100, and then a callback that will receive each 100 Comment objects:

24 lines src/DataFixtures/CommentFixture.php
... lines 1 - 4
use App\Entity\Comment;
... lines 6 - 7
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
... lines 13 - 18
});
$manager->flush();
}
}

Inside, let's use Faker - which we also setup in the last tutorial - to give us awesome, fake data. Start with $comment->setContent(). I'll use multiple lines:

24 lines src/DataFixtures/CommentFixture.php
... lines 1 - 7
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
$comment->setContent(
... line 14
);
... lines 16 - 18
});
... lines 20 - 21
}
}

Now, if $this->faker->boolean, which will be a random true or false, then either generate a random paragraph: $this->faker->paragraph, or generate two random sentences. Pass true to get this as text, not an array:

24 lines src/DataFixtures/CommentFixture.php
... lines 1 - 7
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
$comment->setContent(
$this->faker->boolean ? $this->faker->paragraph : $this->faker->sentences(2, true)
);
... lines 16 - 18
});
... lines 20 - 21
}
}

Cool! Next, for the author, we can use $comment->setAuthor() with $this->faker->name, to get a random person's name:

24 lines src/DataFixtures/CommentFixture.php
... lines 1 - 7
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
$comment->setContent(
$this->faker->boolean ? $this->faker->paragraph : $this->faker->sentences(2, true)
);
$comment->setAuthorName($this->faker->name);
... line 18
});
... lines 20 - 21
}
}

By the way, all of these faker functions are covered really well in their docs. I'm seriously not just making them up.

Finally, add $comment->setCreatedAt() with $this->faker->dateTimeBetween() from -1 months to -1 seconds:

24 lines src/DataFixtures/CommentFixture.php
... lines 1 - 7
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
$comment->setContent(
$this->faker->boolean ? $this->faker->paragraph : $this->faker->sentences(2, true)
);
$comment->setAuthorName($this->faker->name);
$comment->setCreatedAt($this->faker->dateTimeBetween('-1 months', '-1 seconds'));
});
... lines 20 - 21
}
}

That'll give us much more interesting data.

Using the Reference System

At this point, this is a valid Comment object... we just haven't related it to an Article yet. We know how to do this, but... the problem is that all of the articles are created in a totally different fixture class. How can we get access to them here?

Well, one solution would be to use the entity manager, get the ArticleRepository, and run some queries to fetch out the articles.

But, that's kinda lame. So, there's an easier way. Look again at the BaseFixture class, specifically, the createMany() method:

40 lines src/DataFixtures/BaseFixture.php
... lines 1 - 9
abstract class BaseFixture extends Fixture
{
... lines 12 - 27
protected function createMany(string $className, int $count, callable $factory)
{
for ($i = 0; $i < $count; $i++) {
$entity = new $className();
$factory($entity, $i);
$this->manager->persist($entity);
// store for usage later as App\Entity\ClassName_#COUNT#
$this->addReference($className . '_' . $i, $entity);
}
}
}

It's fairly simple, but it does have one piece of magic: it calls $this->addReference() with a key, which is the entity class name, an underscore, then an integer that starts at zero and counts up for each loop. For the second argument, it passes the object itself.

This reference system is a little "extra" built into Doctrine's fixtures library. When you add a "reference" from one fixture class, you can fetch it out in another class. It's super handy when you need to relate entities. And hey, that's exactly what we're trying to do!

Inside CommentFixture, add $comment->setArticle(), with $this->getReference() and pass it one of those keys: Article::class, then _0:

26 lines src/DataFixtures/CommentFixture.php
... lines 1 - 4
use App\Entity\Article;
... lines 6 - 8
class CommentFixture extends BaseFixture
{
protected function loadData(ObjectManager $manager)
{
$this->createMany(Comment::class, 100, function(Comment $comment) {
... lines 14 - 19
$comment->setArticle($this->getReference(Article::class.'_0'));
});
... lines 22 - 23
}
}

PhpStorm is complaining about a type-mismatch, but this will totally work. Try it! Find your terminal and run:

php bin/console doctrine:fixtures:load

No errors! That's a great sign! Check out the database:

php bin/console doctrine:query:sql 'SELECT * FROM comment'

Yes! 100 comments, and each is related to the exact same article.

Relating to Random Articles

So, success! Except that this isn't very interesting yet. All our comments are related to the same one article? Come on!

Let's spice things up by relating each comment to a random article. And, learn about when we need to implement a DependentFixtureInterface.

Leave a comment!

  • 2018-06-25 Victor Bocharsky

    Hey Daniel,

    Yeah, you missed the namespace, add "use App\Entity\Article;" line in the beginning of the src/DataFixtures/CommentFixture.php file.

    Cheers!

  • 2018-06-24 Daniel

    When I run "php bin/console doctrine:fixtures:load", I get:

    In ReferenceRepository.php line 141:

    Reference to: (App\DataFixtures\Article_0) does not exist

    However, doing some tiny debugging, I found out that if I change this line:
    $comment->setArticle($this->getReference(Article::class.'_0'));
    to:
    $comment->setArticle($this->getReference('App\Entity\Article_0'));

    It totally works. Am I missing something?