Join Entity App Refactoring

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

In some ways, not much just changed. Before, we had a genus_scientist table with genus_id and user_id columns. And... we still have that, just with two new columns:

... lines 1 - 10
class GenusScientist
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
... lines 19 - 31
/**
* @ORM\Column(type="string")
*/
private $yearsStudied;
... lines 36 - 70
}

But, in our app, a ton just changed. That's my nice way of saying: we just broke everything!

Collection of GenusScientists, not Users

For example, before, genusScientists was a collection of User objects, but now it's a collection of GenusScientist objects:

... lines 1 - 14
class Genus
{
... lines 17 - 71
/**
* @ORM\OneToMany(targetEntity="GenusScientist", mappedBy="genus", fetch="EXTRA_LAZY")
*/
private $genusScientists;
... lines 76 - 202
}

The same thing is true on User:

... lines 1 - 16
class User implements UserInterface
{
... lines 19 - 77
/**
* @ORM\OneToMany(targetEntity="GenusScientist", mappedBy="user")
*/
private $studiedGenuses;
... lines 82 - 241
}

Wherever our code was using the studiedGenuses property - to get the collection or change it - well, that code is done broke.

Let's clean things up! And see some cool stuff along the way.

First, because we just emptied our database, we have no data. Open the fixtures file and temporarily comment-out the genusScientists property:

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

We can't simply set a User object on genusScientists anymore: this now accepts GenusScientist objects. We'll fix that in a second.

But, run the fixtures:

./bin/console doctrine:fixtures:load

While that's working, go find GenusController and newAction(). Let's once again use this method to hack together and save some interesting data.

First, remove the two addGenusScientist lines:

... lines 1 - 13
class GenusController extends Controller
{
... lines 16 - 18
public function newAction()
{
... lines 21 - 40
$genus->addGenusScientist($user);
$genus->addGenusScientist($user); // duplicate is ignored!
... lines 43 - 52
}
... lines 54 - 146
}

These don't make any sense anymore!

How can we add a new row to our join table? Just create a new entity: $genusScientist = new GenusScientist(). Then, set $genusScientist->setGenus($genus), $genusScientist->setUser($user) and $genusScientist->setYearsStudied(10). Don't forget to $em->persist() this new entity:

... lines 1 - 6
use AppBundle\Entity\GenusScientist;
... lines 8 - 14
class GenusController extends Controller
{
... lines 17 - 19
public function newAction()
{
... lines 22 - 42
$genusScientist = new GenusScientist();
$genusScientist->setGenus($genus);
$genusScientist->setUser($user);
$genusScientist->setYearsStudied(10);
$em->persist($genusScientist);
... lines 48 - 57
}
... lines 59 - 151
}

There's nothing fancy going on anymore: GenusScientist is a normal, boring entity.

Using the new Collections

In your browser, try it: head to /genus/new. Genus created! Click the link to see it! Explosion! That's no surprise: our template code is looping over genusScientists and expecting a User object. Silly template! Let's fix that and the fixtures next.

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
    }
}