Buy
Buy

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!

  • 2019-05-24 Diego Aguiar

    Cool! Thanks for sharing it :)

  • 2019-05-24 Mirko

    Yes, I managed to do it with Doctrine by following this: https://www.doctrine-projec...
    Down below are the annotations I used for the GenusScientist entity's primary key. For the remainder of the tutorial I just needed to do some minor adjustments, but otherwise everything went well.

    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="NONE")
    * @ORM\ManyToOne(targetEntity="Genus", inversedBy="genusScientist")
    * @ORM\JoinColumn(nullable=false)
    */
    private $genus;

    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="NONE")
    * @ORM\ManyToOne(targetEntity="User", inversedBy="studiedGenuses")
    * @ORM\JoinColumn(nullable=false)
    */
    private $user;

  • 2019-05-22 Diego Aguiar

    Ohh, you have a good point and probably from a DB point of view that would be the way to go but to be honest I haven't done that on Doctrine, so, I'm not totally sure if it's supported or not (Maybe yes?). If you find more info about it please let me know :)

    Cheers!

  • 2019-05-21 Mirko

    Ok, so I can put a unique constraint on the genus_id-user_id combination if I want to. But, as I understand, GenusScientist table (entity) must not have the same combination of genus_id and user_id more than once, since this table acts like an intermediary table in many-to-many relationship between Genus and User.
    My question is, if we make a combination of genus_id and user_id a primary key, then we don't need the surrogate id nor do we need to put a unique constraint on genus_id-user_id combination, since primary key is unique by default?

  • 2019-05-21 Diego Aguiar

    Hey @Mirko

    That's a good point and it actually depends on your data constraints, if you have decided that you don't want to have duplicated combinations then you can set up unique constraints at the DB level. Check this out: https://www.doctrine-projec...

    Cheers!

  • 2019-05-21 Mirko

    Wouldn't it be better to have a composite primary key for the GenusScientist entity, consisting of two foreign keys (genus_id, user_id), since this way, with surrogate primary key (id), theoretically we can end up with the same two combinations of foreign keys (genus_id, user_id) under two different primary keys (id)?

  • 2018-06-25 Diego Aguiar

    Excellent, I'm ready! (cracking knuckles)

  • 2018-06-23 Krzysztof Krakowiak

    Diego Aguiar thanks :) I will do some tests to see hoe it works, maybe I will comeback here with more questions.

  • 2018-06-22 Diego Aguiar

    Hey Krzysztof Krakowiak

    Very interesting question! Honestly, I haven't had the need (or obligation) of using a composite primary key for my entities, but I found a nice article about how to do it: https://www.doctrine-projec...

    I hope it helps. Cheers!

  • 2018-06-22 Krzysztof Krakowiak

    Do we have to keep field: id as a primary key for GenusScientist? What implictaions will be here if I will remove it and add composite primary key?