Buy

The King of Relations: ManyToOne

Selecting between ManyToOne and ManyToMany

Each genus will have many genus_notes. But, each genus_note that someone adds will relate to only one genus. There are only two possible types of relationships, and this is by far the most common. It's called a ManyToOne association.

The second type is called a ManyToMany association. To use a different example, this would be if each product had many tags, but also each tag related to many products.

And when it comes to Doctrine relations - don't trust the Internet! Some people will try to confuse you with other relationships like OneToMany, OneToOne and some garbage about unidirectional and bidirectional associations. Gross. Ignore it all. I guarantee, all of that will make sense really soon.

So your first job is simple: decide if you have a ManyToOne or ManyToMany relationship. And it's easy. Just answer this question:

Do either of the sides of the relationship belong to only one of the other?

Each genus_note belongs to only one genus, so we have a classic ManyToOne relationship.

Setting up a ManyToOne Relation

Forget about Doctrine: just think about the database. If every genus_note should belong to exactly one genus, How would you set that up? You'd probably add a genus_id column to the genus_note table. Simple!

Since we need to add a new column to GenusNote, open that entity class. You probably feel like you want to add a $genusId integer property here. That makes sense. But don't! Instead, add a $genus property and give it a ManyToOne annotation. Inside that, add targetEntity="Genus":

95 lines src/AppBundle/Entity/GenusNote.php
... lines 1 - 10
class GenusNote
{
... lines 13 - 39
/**
* @ORM\ManyToOne(targetEntity="Genus")
*/
private $genus;
... lines 44 - 93
}

Tip

You can also use the full namespace: AppBundle\Entity\Genus - and that's required if the two entities do not live in the same namespace/directory.

Umm, guys? That's it. Relationship finished. Seriously.

Leave a comment!

  • 2016-06-14 JLChafardet

    I cant believe it! wow

    @ORM\ManyToOne(targetEntity="EntityName") <- SERIOUSLY?????? what have I done this past years? oh gawd, gimme my time back.

    ..... i want to go cry in a corner for a long while

  • 2016-06-14 weaverryan

    Yo!

    Yep, this is a perfect case for OneToOne. I dismiss OneToOne here, because OneToOne is really the same in the database as a ManyToOne, except that Doctrine makes user_id on the profile table unique, to prevent you from creating multiple. I want people to focus on the big two: ManyToOne and ManyToMany. Also, sometimes, OneToOne can give you more work. In this example, you *could* simply just put all the field on Profile onto User, and eliminate the complexity. Of course, then the User table becomes bigger and bigger, so splitting this into a OneToOne situation is legitimate (but sometimes I see people over-doing this and adding complexity for no benefit).

    So yes, you're in find shape :)

  • 2016-06-14 the_nuts

    Hi,

    I have an Entity User (with id, username, email, is_active, and other most used parameters)
    and another entity Profile (with personal info such as first and last name, address, etc... which are rarely used in the application)
    The PK ot the table profiles should be user_id, with users.id as FK.

    Can't I use OneToOne in this case?

  • 2016-05-31 weaverryan

    Hey Dan!

    Wow, you have a *very* good attention for detail! The truth is that nothing has changed - OneToMany still exists. In truth, you only *need* ManyToOne and ManyToMany. OneToMany is an optional way to look at a ManyToOne relationship. So really, ManyToOne and OneToMany are the *same* relationship, just looked to from different sides. So, I like to tell people initially just to worry about ManyToOne and ManyToMany. Later, we will learn about OneToMany, but it isn't really a true, new relationship type.

    Also, there is another - OneToOne. But again, this is *really* just a ManyToOne (it looks the same in the database), but Doctrine helps you to guarantee that you only link one object to one other object. There is limited use for this relationship in my opinion, so I save it for later :).

    Short answer: there is no difference! Just me simplifying how we explain this.

    Cheers!

  • 2016-05-27 Dan Costinel

    Hey Ryan,

    I'm having the old video, the one presented by Leanna, in which she says there are 3 (three) types of relationships: ManyToOne, ManyToMany and OneToMany. I know her version of Symfony, back then, was 2.4, and now, you are presenting this tutorial for Symfony 3, with just 2 (two) types of relationships: ManyToOne and ManyToMany.

    My question: the difference between the number of relationships is because of the Symfony different versions?

    Thank you.

  • 2016-03-23 weaverryan

    Hopefully this week - but definitely by next week :)

  • 2016-03-23 Andrew Grudin

    When?