Foundry: Always Pass a Factory Instance to a Relation

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

I love Foundry. But using Foundry with Doctrine relationships is probably the hardest part of this library. So let's push a bit further. Pretend that, in this situation, we want to override the question value. Right now it grabs any random Question from the database. But I want to randomly grab only one of these 20 published questions.

Overriding the question Property

No problem! And this part is pretty manual. Put our callback... back... and return an array. There actually is a way in Foundry, to say:

please give me a random Question where some field matches some value.

But... in our case, we would need to say WHERE askedAt IS NOT NULL... which is too complex for that system to handle. But no worries! We'll just do this manually.

Above, on the createMany() call, add a $questions = before this. Back down here, add a use to the callback so that the $questions variable is accessible... then leverage array_rand() to grab a random item.

... lines 1 - 11
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$questions = QuestionFactory::createMany(20);
... lines 17 - 23
AnswerFactory::createMany(100, function() use ($questions) {
return [
'question' => $questions[array_rand($questions)]
];
});
... lines 29 - 30
}
}

Let's make sure this works! Reload the fixtures and...

symfony console doctrine:fixtures:load

No errors! We can use a special query to check this:

SELECT DISTINCT question_id FROM answer

symfony console doctrine:query:sql 'SELECT DISTINCT question_id FROM answer'

Yes! The answers are related to exactly 20 questions.

Accidentally Creating Extra Relation Objects

That was... manual but simple enough. And it was a great setup to show you a really common mistake when using Foundry with relationships.

In AnswerFactory, let's change the default question to create a new unpublished question. We can do this by saying QuestionFactory::new() - to create a QuestionFactory object - then ->unpublished().

There's no magic here: unpublished() is a method we created in the first tutorial: it changes the askedAt value to null. Then, to actually create the Question from the factory, add ->create().

... lines 1 - 28
final class AnswerFactory extends ModelFactory
{
... lines 31 - 37
protected function getDefaults(): array
{
return [
... lines 41 - 44
'question' => QuestionFactory::new()->unpublished()->create(),
];
}
... lines 48 - 60
}

This is totally legal: it will create a new unpublished Question, save it to the database and then that Question will be used as the question key when creating the Answer.

Well, that's what would normally happen. But since we are overriding the question key, this change should make absolutely no difference in our situation.

Famous last words. Reload the fixtures:

symfony console doctrine:fixtures:load

No errors... but check out how many questions there are in the database:

SELECT * from question

symfony console doctrine:query:sql 'SELECT * from question'

We should have 20+5: 25 questions. Instead... we have 125!

The problem is subtle... but maybe you spotted it! We're creating 100 answers... and the getDefaults() method is called for every one. That's.... good! But the moment that this question line is executed, it creates a new unpublished Question and saves it to the database. Then... a moment later, the question is overridden. This means that the 100 answers were all, in the end, correctly related to one of the 20 published questions. But it also means that, along the way, 100 extra questions were created, saved to the database... then never used.

What's the fix? Simple: remove ->create().

... lines 1 - 28
final class AnswerFactory extends ModelFactory
{
... lines 31 - 37
protected function getDefaults(): array
{
return [
... lines 41 - 44
'question' => QuestionFactory::new()->unpublished(),
];
}
... lines 48 - 60
}

This means that the question key is now set to a QuestionFactory object. The new() method returns a new QuestionFactory instance... and then the unpublished() method return self: so it returns that same QuestionFactory object.

Setting a relation property to a factory instance is totally allowed. In fact, you should always set a relation property to a factory instance if you can. Why?

Because this allows Foundry to delay creating the Question object until later. And in this case, it realizes that the question has been overridden, and so it avoids creating the extra object entirely... which is perfect.

Reload the fixtures one more time:

symfony console doctrine:fixtures:load

And check the question table:

symfony console doctrine:query:sql 'SELECT * from question'

We're back to 25 rows.

Next: let's use the new relationship to render answers on the frontend.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.4.1 || ^8.0.0",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^3.3", // v3.3.0
        "composer/package-versions-deprecated": "^1.11", // 1.11.99.3
        "doctrine/doctrine-bundle": "^2.1", // 2.4.2
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
        "doctrine/orm": "^2.7", // 2.9.5
        "knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
        "knplabs/knp-time-bundle": "^1.11", // v1.16.1
        "pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
        "pagerfanta/twig": "^3.3", // v3.3.0
        "sensio/framework-extra-bundle": "^6.0", // v6.1.5
        "stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
        "symfony/asset": "5.3.*", // v5.3.4
        "symfony/console": "5.3.*", // v5.3.7
        "symfony/dotenv": "5.3.*", // v5.3.7
        "symfony/flex": "^1.3.1", // v1.15.1
        "symfony/framework-bundle": "5.3.*", // v5.3.7
        "symfony/monolog-bundle": "^3.0", // v3.7.0
        "symfony/runtime": "5.3.*", // v5.3.4
        "symfony/stopwatch": "5.3.*", // v5.3.4
        "symfony/twig-bundle": "5.3.*", // v5.3.4
        "symfony/webpack-encore-bundle": "^1.7", // v1.12.0
        "symfony/yaml": "5.3.*", // v5.3.6
        "twig/extra-bundle": "^2.12|^3.0", // v3.3.1
        "twig/string-extra": "^3.3", // v3.3.1
        "twig/twig": "^2.12|^3.0" // v3.3.2
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
        "symfony/debug-bundle": "5.3.*", // v5.3.4
        "symfony/maker-bundle": "^1.15", // v1.33.0
        "symfony/var-dumper": "5.3.*", // v5.3.7
        "symfony/web-profiler-bundle": "5.3.*", // v5.3.5
        "zenstruck/foundry": "^1.1" // v1.13.1
    }
}