title: Save related Object Symfony 2
body: <p>I have User and Posts. One To Many</p>
  
  <p>//user.php</p>
  
  <pre><code> /**
   * @ORM\OneToMany(targetEntity="Post", mappedBy="posts")
   */
  private $posts;
  
  public function __construct () {
      $this-&gt;posts = new ArrayCollection();
  }
  </code></pre>
  
  <p>post.php</p>
  
  <pre><code>/**
   * @ORM\ManyToOne(targetEntity="User", inversedBy="posts")
   * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
   */
  private $posts;
  </code></pre>
  
  <p>controller</p>
  
  <pre><code>$post-&gt;setPosts($this-&gt;getUser());
  $em-&gt;persist($post);
  $em-&gt;flush();
  </code></pre>
  
  <p>I have to use setPosts for persist related user? Can I save with cascade or something of this?</p>
  
  <p>I know that</p>
  
  <blockquote>
    <p>There are two kinds of cascades in Doctrine:</p>
    
    <p>1) ORM level </p>
    
    <p>2) Database level </p>
  </blockquote>
  
  <p>But I don't understand when use <code>cascade={"persist"})</code></p>
  
  <p>My questions are:</p>
  
  <ul>
  <li>How can I save user related from post object?</li>
  <li>When and Why use
  <code>cascade={"persist"})</code>?</li>
  </ul>
  
  <p><strong>update</strong></p>
  
  <p>I tried</p>
  
  <p>//user</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Post", mappedBy="posts",cascade={"persist"})
   */
  private $posts;
  </code></pre>
  
  <p>controller</p>
  
  <pre><code> $user = $this-&gt;getUser();
   $user-&gt;addPost($post);
   $em-&gt;persist($user);
   $em-&gt;flush();
  </code></pre>
  
  <p>But doesn't works, the field user_id in database is empty</p>