title: Doctrine many-to-many relations and onFlush event
body: <p>Little example about Books and Authors:
  DB structure:</p>
  
  <p><img src="https://i.stack.imgur.com/FpnTh.png" alt="enter image description here"></p>
  
  <p>Entities (they were generated form database using symfony2 console commands):</p>
  
  <p>Author:</p>
  
  <pre><code>    /**
   * Author
   *
   * @ORM\Table(name="authors")
   * @ORM\Entity
   */
  class Author
  {
  
      private $_isCachable = true;
  
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255, nullable=false)
       */
      private $name;
  
      /**
       * @var \Doctrine\Common\Collections\Collection
       *
       * @ORM\ManyToMany(targetEntity="App\CoreBundle\Entity\Books", mappedBy="author", cascade={"persist","remove","detach","merge","refresh"})
       */
      private $book;
  
      /**
       * Constructor
       */
      public function __construct()
      {
      $this-&gt;book = new \Doctrine\Common\Collections\ArrayCollection();
      }
  }
  </code></pre>
  
  <p>Book:</p>
  
  <pre><code> &lt;?php
  
  namespace App\CoreBundle\Entity;
  
  use App\CoreBundle\RedisLayer\Marks\Insert;
  use App\CoreBundle\RedisLayer\Marks\Update;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Book
   *
   * @ORM\Table(name="books")
   * @ORM\Entity
   */
  class Book
  {
  
      private $_isCachable = true;
  
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="ttile", type="string", length=255, nullable=false)
       */
      private $ttile;
  
      /**
       * @var string
       *
       * @ORM\Column(name="isbn", type="string", length=255, nullable=false)
       */
      private $isbn;
  
      /**
       * @var \Doctrine\Common\Collections\Collection
       *
       * @ORM\ManyToMany(targetEntity="App\CoreBundle\Entity\Authors", inversedBy="book", cascade={"persist","remove","detach","merge","refresh"})
       * @ORM\JoinTable(name="author_books",
       *   joinColumns={
       *     @ORM\JoinColumn(name="book_id", referencedColumnName="id")
       *   },
       *   inverseJoinColumns={
       *     @ORM\JoinColumn(name="author_id", referencedColumnName="id")
       *   }
       * )
       */
      private $author;
  
      /**
       * Constructor
       */
      public function __construct()
      {
      $this-&gt;author = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
  }
  </code></pre>
  
  <p>So when I run this code:</p>
  
  <pre><code>    $book = new \App\CoreBundle\Entity\Book();
      $book-&gt;setTtile('book title: '.uniqid());
      $book-&gt;setIsbn('book isbn: '.uniqid());
  
      $author = new \App\CoreBundle\Entity\Author();
      $author-&gt;setName('author title: '.uniqid());
  
  
      $author-&gt;addBook($book);
  
      $entityManager-&gt;persist($author);
  
      $entityManager-&gt;flush();
  </code></pre>
  
  <p>I expect doctrine to generate these SQLs:</p>
  
  <pre><code>"START TRANSACTION"]
  
      # CREATE BOOK
      INSERT INTO books (ttile, isbn) VALUES (?, ?)
      array(2) {
        [1] =&gt;
        string(25) "book title: 524ea3cf34d5f"
        [2] =&gt;
        string(24) "book isbn: 524ea3cf34da3"
      }
      array(2) {
        [1] =&gt;
        string(6) "string"
        [2] =&gt;
        string(6) "string"
      }
  
      # CREATE AUTHOR
      INSERT INTO authors (name) VALUES (?)
      array(1) {
        [1] =&gt;
        string(27) "author title: 524ea3cf34de9"
      }
      array(1) {
        [1] =&gt;
        string(6) "string"
      }
  
      # CREATE LINKS
      INSERT INTO author_books (book_id, author_id) VALUES (?, ?)
      array(2) {
        [0] =&gt;
        int(34)
        [1] =&gt;
        int(23)
      }
  
  "COMMIT"
  </code></pre>
  
  <p>But i get only SQLs  for #CERATE AUTHOR and #CREATE BOOK. SQL for linking table was not generated.</p>
  
  <ol>
  <li><p>What is the problem?</p></li>
  <li><p>If I run this code:</p>
  
  <pre><code>$book = new \App\CoreBundle\Entity\Book();
  $book-&gt;setTtile('book title: '.uniqid());
  $book-&gt;setIsbn('book isbn: '.uniqid());
  
  $author = new \App\CoreBundle\Entity\Author();
  $author-&gt;setName('author title: '.uniqid());
  
  $author-&gt;addBook($book);
  $book-&gt;addAuthor($author); // added
  
  $entityManager-&gt;persist($author);
  
  $entityManager-&gt;flush();
  </code></pre>
  
  <p>all is OK. So I need to add book to author and author to book? Is it possible to create book and author and then and book to author and flush (without adding author to book) ?</p></li>
  <li><p>If I use doctrine onFLush event listener I can use <code>getScheduledEntityUpdates</code> (list of entities that should be updated), <code>getScheduledEntityInsertions</code> (list of entities that should be updated) and <code>getScheduledEntityDeletions</code> (list of entities that should be deleted) methods of the unitOfWork object. But this is only entities. How can I get info about linking table?  I need to get SQL for linking table at doctrine onFlush event.</p></li>
  </ol>