Understanding the @ManyToMany annotation with tags
Tags group posts by topics. A tag contains several posts, and a post has several tags. This is a Many-To-Many bidirectional association. Doctrine manages transparently the association table needed to store Many-To-Many relations at the SQL level. The MySQL schema that will be generated is shown in the following screenshot:
Creating the Tag entity class (inverse side)
The Tag
entity class has only two properties:
name
: This is the name of the tag, it is unique, and is the identifier of the entityposts
: This is the collection of posts associated with this tag
The following are the steps to create the Tag
entity class:
Create a
Tag.php
file in thesrc/Blog/Entity/
location that contains the entity class using the following code snippet:<?php namespace Blog\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\ManyToMany...