ManyToMany & Fixtures

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

Head back to /genus. These genuses are coming from our fixtures, but, sadly, the fixtures don't relate any scientists to them... yet. Let's fix that!

The fixtures.yml creates some Genus objects and some User objects, but nothing links them together:

AppBundle\Entity\Genus:
genus_{1..10}:
name: <genus()>
subFamily: '@subfamily_*'
speciesCount: <numberBetween(100, 100000)>
funFact: <sentence()>
isPublished: <boolean(75)>
firstDiscoveredAt: <dateTimeBetween('-200 years', 'now')>
... lines 9 - 22
AppBundle\Entity\User:
user_{1..10}:
email: weaverryan+<current()>@gmail.com
plainPassword: iliketurtles
roles: ['ROLE_ADMIN']
avatarUri: <imageUrl(100, 100, 'abstract')>
user.aquanaut_{1..10}:
email: aquanaut<current()>@example.org
plainPassword: aquanote
isScientist: true
firstName: <firstName()>
lastName: <lastName()>
universityName: <company()> University
avatarUri: <imageUrl(100, 100, 'abstract')>

How can we do that? Well, remember, the fixtures system is very simple: it sets each value on the given property. It also has a super power where you can use the @ syntax to reference another object:

AppBundle\Entity\Genus:
genus_{1..10}:
... line 3
subFamily: '@subfamily_*'
... lines 5 - 37

In that case, that other object is set on the property.

Setting data on our ManyToMany is no different: we need to take a Genus object and set an array of User objects on the genusScientists property. In other words, add a key called genusScientists set to [] - the array syntax in YAML. Inside, use @user.aquanaut_1. That refers to one of our User objects below. And whoops, make sure that's @user.aquanaut_1. Let's add another: @user.aquanaut_5:

AppBundle\Entity\Genus:
genus_{1..10}:
... lines 3 - 8
genusScientists: ['@user.aquanaut_1', '@user.aquanaut_5']
... lines 10 - 38

It's not very random... but let's try it! Find your terminal and run:

./bin/console doctrine:fixtures:load

Ok, check out the /genus page. Now every genus is related to the same two users.

Smart Fixtures: Using the Adder!

But wait... that should not have worked. The $genusScientists property - like all of these properties is private. To set them, the fixtures library uses the setter methods. But, um, we don't have a setGenusScientists() method, we only have addGenusScientist():

... lines 1 - 14
class Genus
{
... lines 17 - 174
public function addGenusScientist(User $user)
{
if ($this->genusScientists->contains($user)) {
return;
}
$this->genusScientists[] = $user;
}
... lines 183 - 195
}

So that's just another reason why the Alice fixtures library rocks. Because it says:

Hey! I see an addGenusScientist() method! I'll just call that twice instead of looking for a setter.

Randomizing the Users

The only way this could be more hipster is if we could make these users random. Ah, but Alice has a trick for that too! Clear out the array syntax and instead, in quotes, say 3x @user.aquanaut_*:

AppBundle\Entity\Genus:
genus_{1..10}:
... lines 3 - 8
genusScientists: '3x @user.aquanaut_*'
... lines 10 - 38

Check out that wonderful Alice syntax! It says: I want you to go find three random users, put them into an array, and then try to set them.

Reload those fixtures!

./bin/console doctrine:fixtures:load

Then head over to your browser and refresh. Cool, three random scientists for each Genus. Pretty classy Alice, pretty classy.

Leave a comment!

This course is built on Symfony 3, but most of the concepts apply just fine to newer versions of Symfony.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
        "doctrine/doctrine-migrations-bundle": "^1.1", // 1.1.1
        "stof/doctrine-extensions-bundle": "^1.2" // v1.2.2
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0", // v3.1.3
        "nelmio/alice": "^2.1", // 2.1.4
        "doctrine/doctrine-fixtures-bundle": "^2.3" // 2.3.0
    }
}