Login to bookmark this video
Buy Access to Course

ManyToMany & Fixtures

Share this awesome video!


Keep on Learning!

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

Login Subscribe

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:

name: <genus()>
subFamily: '@subfamily_*'
speciesCount: <numberBetween(100, 100000)>
funFact: <sentence()>
isPublished: <boolean(75)>
firstDiscoveredAt: <dateTimeBetween('-200 years', 'now')>
// ... lines 9 - 22
email: weaverryan+<current()>@gmail.com
plainPassword: iliketurtles
roles: ['ROLE_ADMIN']
avatarUri: <imageUrl(100, 100, 'abstract')>
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:

// ... 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:

// ... 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():

197 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 14
class Genus
// ... lines 17 - 174
public function addGenusScientist(User $user)
if ($this->genusScientists->contains($user)) {
$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_*:

// ... 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.