Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

The King of Relations: ManyToOne

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

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":

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

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!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

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
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
        "doctrine/doctrine-migrations-bundle": "^1.1" // 1.1.1
    },
    "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
    }
}
userVoice