title: symfony 2 Fixtures for many to many relationship
body: <p>How to set up fixtures for symfony 2 in for many to many relationship,
  Following 2 entities are made by command lines,
  And then added some lines for many to many relationship</p>
  
  <p><strong>Entity 1: Blog Class</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Acme\MainBundle\Entity;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Blog
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Acme\MainBundle\Entity\BlogRepository")
  */
   class Blog {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @ORM\ManyToMany(targetEntity="tag", inversedBy="blogs")
   * @ORM\JoinTable(name="blog_tag",
   *      joinColumns={@ORM\JoinColumn(name="blog_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
   *      )
   **/
  protected $tags;
  
  public function __construct()
  {
      $this-&gt;tags = new ArrayCollection();
  }
  
  
  /**
   * @var string
   *
   * @ORM\Column(name="title", type="string", length=255)
   */
  private $title;
  
  /**
   * @var string
   *
   * @ORM\Column(name="subTitle", type="text")
   */
  private $subTitle;
  
  /**
   * @var string
   *
   * @ORM\Column(name="author", type="string", length=255)
   */
  
  private $author;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="createdAt", type="datetime")
   */
  private $createdAt;
  
  /**
   * @var \DateTime
   *
   * @ORM\Column(name="updatedAt", type="datetime")
   */
  private $updatedAt;
  
  /**
   * @var string
   *
   * @ORM\Column(name="content", type="text")
   */
  private $content;
  
  /**
   * @var boolean
   *
   * @ORM\Column(name="isPublished", type="boolean")
   */
  private $isPublished;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId() {
      return $this-&gt;id;
  }
  
  /**
   * Set title
   *
   * @param string $title
   * @return Blog
   */
  public function setTitle($title) {
      $this-&gt;title = $title;
  
      return $this;
  }
  
  /**
   * Get title
   *
   * @return string 
   */
  public function getTitle() {
      return $this-&gt;title;
  }
  
  /**
   * Set author
   *
   * @param string $author
   * @return Blog
   */
  public function setAuthor($author) {
      $this-&gt;author = $author;
  
      return $this;
  }
  
  /**
   * Get author
   *
   * @return string 
   */
  public function getAuthor() {
      return $this-&gt;author;
  }
  
  /**
   * Set createdAt
   *
   * @param \DateTime $createdAt
   * @return Blog
   */
  public function setCreatedAt($createdAt) {
      $this-&gt;createdAt = $createdAt;
  
      return $this;
  }
  
  /**
   * Get createdAt
   *
   * @return \DateTime 
   */
  public function getCreatedAt() {
      return $this-&gt;createdAt;
  }
  
  /**
   * Set updatedAt
   *
   * @param \DateTime $updatedAt
   * @return Blog
   */
  public function setUpdatedAt($updatedAt) {
      $this-&gt;updatedAt = $updatedAt;
  
      return $this;
  }
  
  /**
   * Get updatedAt
   *
   * @return \DateTime 
   */
  public function getUpdatedAt() {
      return $this-&gt;updatedAt;
  }
  
  /**
   * Set content
   *
   * @param string $content
   * @return Blog
   */
  public function setContent($content) {
      $this-&gt;content = $content;
  
      return $this;
  }
  
  /**
   * Get content
   *
   * @return string 
   */
  public function getContent() {
      return $this-&gt;content;
  }
  
  /**
   * Set isPublished
   *
   * @param boolean $isPublished
   * @return Blog
   */
  public function setIsPublished($isPublished) {
      $this-&gt;isPublished = $isPublished;
  
      return $this;
  }
  
  /**
   * Get isPublished
   *
   * @return boolean 
   */
  public function getIsPublished() {
      return $this-&gt;isPublished;
  }
  
  public function __toString() {
      return strval($this-&gt;id);
  }
  
  
  /**
   * Set subTitle
   *
   * @param string $subTitle
   * @return Blog
   */
  public function setSubTitle($subTitle)
  {
      $this-&gt;subTitle = $subTitle;
  
      return $this;
  }
  
  /**
   * Get subTitle
   *
   * @return string 
   */
  public function getSubTitle()
  {
      return $this-&gt;subTitle;
  }
  
  /**
   * Add tags
   *
   * @param \Acme\MainBundle\Entity\tag $tags
   * @return Blog
   */
  public function addTag(\Acme\MainBundle\Entity\tag $tags)
  {
      $this-&gt;tags[] = $tags;
      $tags-&gt;addBlog($this);
  
      return $this;
  }
  
  /**
   * Remove tags
   *
   * @param \Acme\MainBundle\Entity\tag $tags
   */
  public function removeTag(\Acme\MainBundle\Entity\tag $tags)
  {
      $this-&gt;tags-&gt;removeElement($tags);
  }
  
  /**
   * Get tags
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getTags()
  {
      return $this-&gt;tags;
  }
  }
  </code></pre>
  
  <p><strong>Entity 2: Tag Class</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Acme\MainBundle\Entity;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Tag
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Acme\MainBundle\Entity\TagRepository")
   */
  class Tag {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @ORM\ManyToMany(targetEntity="blog", mappedBy="tags")
   **/
  
  protected $blogs;
  
  /**
   * @var string
   *
   * @ORM\Column(name="tag", type="string", length=255)
   */
  private $tag;
  
  /**
   * @var boolean
   *
   * @ORM\Column(name="isPublished", type="boolean")
   */
  private $isPublished;
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId() {
      return $this-&gt;id;
  }
  
  /**
   * Set tag
   *
   * @param string $tag
   * @return Tag
   */
  public function setTag($tag) {
      $this-&gt;tag = $tag;
  
      return $this;
  }
  
  /**
   * Get tag
   *
   * @return string 
   */
  public function getTag() {
      return $this-&gt;tag;
  }
  
  /**
   * Set isPublished
   *
   * @param boolean $isPublished
   * @return Tag
   */
  public function setIsPublished($isPublished) {
      $this-&gt;isPublished = $isPublished;
  
      return $this;
  }
  
  /**
   * Get isPublished
   *
   * @return boolean 
   */
  public function getIsPublished() {
      return $this-&gt;isPublished;
  }
  
  public function __toString() {
      return strval($this-&gt;id);
  }
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;blogs = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Add blogs
   *
   * @param \Acme\MainBundle\Entity\blog $blogs
   * @return Tag
   */
  public function addBlog(\Acme\MainBundle\Entity\blog $blogs)
  {
      $this-&gt;blogs[] = $blogs;
      $blogs-&gt;addTag($this);
  
  
      return $this;
  }
  
  /**
   * Remove blogs
   *
   * @param \Acme\MainBundle\Entity\blog $blogs
   */
  public function removeBlog(\Acme\MainBundle\Entity\blog $blogs)
  {
      $this-&gt;blogs-&gt;removeElement($blogs);
  }
  
  /**
   * Get blogs
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getBlogs()
  {
      return $this-&gt;blogs;
  }
  }
  </code></pre>