title: Doctrine2 Association for ManyToMany Bidirectional and Insert operation for Unique OneToMany Relationships
body: <p>I am using <code>Symfony2</code> with <code>Doctrine 2</code>.
  I have 5 entities in my application.</p>
  
  <ol>
  <li>Book</li>
  <li>Keyword (Bidirectional, should fetch all books having particular keyword)</li>
  <li>Author (Bidirectional, should fetch all books having particular author(s))</li>
  <li>Category (Bidirectional, should fetch all books falling into particular category)</li>
  <li><p>BookExtra (Unidirectional, In Book Entity, should fetch BookExtra data)</p>
  
  <ul>
  <li>Each book can have many keywords  </li>
  <li>Each book can have many authors</li>
  <li>Each book can fall into a single category</li>
  <li>Each book have exactly one BookExtra record.</li>
  <li>The keyword and author tables should contain unique values</li>
  </ul></li>
  </ol>
  
  <p>When a new <code>Book</code> is added, if the <code>Author(s)</code> and <code>Keyword(s)</code> exists, the respected <code>Author ID</code> and <code>Keyword ID</code> should be assigned to the <code>Book</code>, and if it doesn't exist, the new <code>records</code> will be created and the respected <code>ID</code> should be assigned to the <code>Book</code>.</p>
  
  <p>I have the following <code>Entity Classes</code>:</p>
  
  <pre><code>Book.php
  
  namespace Acme\StoreBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Book
   *
   * @ORM\Table(name="book")
   * @ORM\Entity
   */
  class Book {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="isbn", type="string", length=16)
   */
  protected $isbn;
  
  /**
   * @var string
   *
   * @ORM\Column(name="title", type="string", length=255)
   */
  protected $title;
  
  /*
   * @ORM\OneToOne(targetEntity="BookExtra")
   * @ORM\JoinColumn(name="extra_id", referencedColumnName="id")
   *
   *   *********         OR        *********
   *   *********What should go HERE*********
   *
   */
  protected $bookextra;
  
  /*
   * @ORM\ManyToMany(targetEntity="Author", inversedBy="books")
   * @ORM\JoinTable(name="authors_books")
   */
  protected $author;
  
  /*
   * @ORM\OneToOne(targetEntity="Category")
   * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
   */
  protected $category;
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>BookExtra.php
  
  namespace Acme\StoreBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * BookExtra
   *
   * @ORM\Table(name="book_extra")
   * @ORM\Entity
   */
  class Detail {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="data", type="string", length=255)
   */
  protected $data;
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>Author.php
  
  namespace Bookhut\BookBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Author
   *
   * @ORM\Table(name="author")
   * @ORM\Entity
   */
  class Author {
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=100)
   */
  protected $name;
  
  // **********What should go HERE*********
  protected $books;
  
  }
  </code></pre>
  
  <p><code>Keyword</code> &amp; <code>Category</code> Entities are similar to Author Entity</p>
  
  <p>The Problem is that, when i generate <code>schema</code> with <code>cli</code>, it never generates <code>Relationships/Associations</code>.</p>
  
  <p>And what should be the proper <code>Relationships/Associations</code> for <code>Book &amp; Author</code> Entity</p>
  
  <p>I searched for this problem and proper <code>Relationships/Associations</code></p>
  
  <p>I found this:</p>
  
  <p><a href="https://stackoverflow.com/questions/14091153/symfony2-app-console-not-generating-properties-or-schema-updates-for-entity-rela">Symfony2 app/console not generating properties or schema updates for Entity Relationships/Associations</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10931191/saving-onetoone-relation-entities">Saving onetoone relation entities</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10618514/doctrine2-in-a-one-to-many-bidirectional-relationship-how-to-save-from-the-inv">doctrine2: in a one-to-many bidirectional relationship, how to save from the inverse side?</a></p>
  
  <p>But it didn't help me.</p>
  
  <p>Can somebody give an example of this type of <code>Relationships/Associations</code> and <code>insert</code> operations?</p>