title: Doctrine one to many unable to do cascade persist because id is not provided by Doctrine
body: <p>I have an Issue entity and this can have multiple Notes, the Issue Entity looks somewhat like this:</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Note", mappedBy="issue", cascade={"persist"})
   */
   protected $notes;
  
   public function __construct(){
     $this-&gt;notes = new ArrayCollection();
   }
   public function getMostRecentNote(){
       $count = count($this-&gt;notes);
       if($count){
           return $this-&gt;notes[$count-1];
       }
       return "No notes";
   }
   public function getNotes(){
       return $this-&gt;notes;
   }
  
   public function addNote(Note $note){
       $this-&gt;notes[] = $note;
   }
   //add an auto not when status changes
   public function setStatus($status){
      if($status === $this-&gt;status){
          return;
      }
      $note = new Note();
      $note-&gt;setNote_date(new \DateTime());
      $note-&gt;setNote("Status changed from "
        .$this-&gt;status
        ." to ".$status);
      $note-&gt;setIssue($this);
      $this-&gt;addNote($note);
      $this-&gt;status=$status;
  }
  </code></pre>
  
  <p>The Note looks like this:</p>
  
  <pre><code>/**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   *
   */
  protected $id;
  /**
   * @ORM\ManyToOne(targetEntity="Issue", inversedBy="notes", cascade={"persist"})
   * @ORM\JoinColumn(name="issue_id", referencedColumnName="id")
   **/
   protected $issue;
  </code></pre>
  
  <p>When I change the status I get the following error:</p>
  
  <blockquote>
    <p>An exception occurred while executing 'INSERT INTO issue_notes
    (issue_id, note, note_date) VALUES (?, ?, ?)' with params
    {"1":null,"2":"Status changed from reported to in
    process","3":"2014-06-20 11:14:43"}:</p>
    
    <p>SQLSTATE[23000]: Integrity constraint violation: 1048 Column
    'issue_id' cannot be null</p>
  </blockquote>
  
  <p>I've found several questions about this regarding one-to-one relationships but as far as I can see the column names are correct and inverseby as well as mappedby are there.</p>
  
  <p><strong>[solved]</strong></p>
  
  <p>Problem was in the Note getters and setters for issue:</p>
  
  <pre><code>public function getIssue()
  {
      return $this-&gt;issue;
  }
  public function setIssue($issue)
  {
      $this-&gt;issue=$issue;
  }
  </code></pre>
  
  <p>the setter was <code>$this-&gt;issues</code>, changed it to the code as above. Of course because Note is the owning side and determines the update I have to set the issue for the note: <code>$note-&gt;setIssue($this);</code> (added the line in the code above). Now if I create a new issue I don't have to explicitly set the ussue_id of the note (I don't know it yet before persisting). Have not tried yet creating a new issue with a note but sure it'll work.</p>
  
  <p><strong>[update]</strong></p>
  
  <p>Almost immediately ran in the following error <code>[Semantical Error] ... Doctrine Class Note has no association named issue_id</code>. I assumed by setting the relation between Note and Issue doctrine would be able to figure out that Note as a field called issue_id but it doesn't. Had to add the following to Note:</p>
  
  <pre><code>/**
   * @var integer $issue_id
   *
   * @ORM\Column(name="issue_id", type="integer")
   *
   */
  protected $issue_id;
  </code></pre>
  
  <p>Now I can run a dql like: <code>SELECT n FROM AcmeDemoBundle:Note n n.issue_id = ?</code></p>