title: QueryBuilder with One To Many relationship and several conditions using Doctrine
body: <p>I will try to explain my issue as well as I can. I have a Message related to another Entity called Reply (one message can have zero or n replies) and I want to take into account the following cases:</p>
  
  <ol>
  <li>If UserA have one message created to UserB (UserA is the creator)
  but the UserB didn't replied the message I don't want get message</li>
  </ol>
  
  <hr>
  
  <ol start="2">
  <li>Same as above but the UserB replied to the message. I want get the
  message with its replies if replies exists</li>
  </ol>
  
  <hr>
  
  <ol start="3">
  <li>If the UserB sent a message to UserA, I want to get the message with
  its replies if replies exists</li>
  </ol>
  
  <p>My Message Entity (I will just put the OneToMany relantionship):</p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  
  /**
  * @ORM\Table(name="messages")
  * @ORM\Entity
  * @ORM\Entity(repositoryClass="AppBundle\Repository\MessageRepository")
  */
  class Message {
  
  /**
  * @ORM\OneToMany(targetEntity="ReplyMessage", mappedBy="message")
  */
  private $replies;
  
  /**
   * Add replies
   *
   * @param \AppBundle\Entity\ReplyMessage $replies
   * @return Message
   */
  public function addReply(\AppBundle\Entity\ReplyMessage $replies)
  {
      $this-&gt;replies[] = $replies;
  
      return $this;
  }
  
  /**
   * Remove replies
   *
   * @param \AppBundle\Entity\ReplyMessage $replies
   */
  public function removeReply(\AppBundle\Entity\ReplyMessage $replies)
  {
      $this-&gt;replies-&gt;removeElement($replies);
  }
  
  /**
   * Get replies
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getReplies()
  {
      return $this-&gt;replies;
  }
  </code></pre>
  
  <p>And the code that I am using to achieve my objective is the following:</p>
  
  <pre><code>$query = $this-&gt;createQueryBuilder('message')
      -&gt;where('message.creator = :username or message.receiver = :username')
      -&gt;leftJoin('message.replies', 'replies')
      -&gt;andWhere('replies.user = :username')
      -&gt;setParameter('username', $username)
      -&gt;getQuery();
  
  $sent = $query-&gt;getResult();
  
  return $sent;
  </code></pre>
  
  <p>I am not an expert building queries with Doctrine and I don't know how can control all my cases right now, if someone could help I'd be very grateful</p>