title: Symfony Doctrine query builder find entity with many to one relation
body: <p>I'm building a website with Symfony for a project which will act like "booking.com" websites but much simplier and with some variations.</p>
  
  <p>Some errors on the database fields but not really important for my issue.</p>
  
  <p><a href="https://i.stack.imgur.com/Ybqmo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ybqmo.png" alt="enter image description here"></a></p>
  
  <p>As you can see, these tables concern two entities : apartment and visit.
  A customer can ask for a visit of an apartment. It is a many to one relationship. </p>
  
  <p>I have a search form to search for apartments with criterias. I want to only show apartments that don't have any visits between the arrival and departure dates that the user provided. 
  So I ended up making a function in apartmentRepository to manage that and other cases.</p>
  
  <p>Problem is: how can I get these apartments ? </p>
  
  <p>Here is a draft of this function which is of course not finished neither perfect (if you have some comments to improve it, it would be great !).</p>
  
  <pre><code>public function findByCustom($parameters): ?array
  {
     $query =  $this-&gt;createQueryBuilder('a');
  
     foreach ($parameters as $key=&gt; $parameter){
         if($key != 'keywords' and $key!= 'priceMin' &amp; $key!='priceMax' and $key!="typeAp" and $key!="searchVisitDate") $query-&gt;orWhere('a.'.$key." = '".$parameter."'");
  
         if($key == "typeAp")
         {
             $typeApQuery = "";
             foreach ($parameters[$key] as $index =&gt; $type)
             {
                 if($index !== count($parameters[$key])-1)
                 {
                     $typeApQuery.=" a.typeAp = '".$type."' or";
                 }
                 else
                 {
                     $typeApQuery.= " a.typeAp = '".$type."'";
                 }
             }
             $query-&gt;andWhere($typeApQuery);
         }
     }
  
     $query-&gt;andWhere('a.rentPrice &gt;='.$parameters['priceMin']." and a.rentPrice &lt;= ".$parameters['priceMax']);
     $withoutInner = $query;
     $query
         -&gt;join('App\Entity\Visit', 'v', Join::ON, 'a = v.apartment')
         -&gt;where("v.date between '2020-03-15' and '2020-03-19'");
      $query-&gt;getQuery()-&gt;getResult();
      $sorted = $withoutInner-&gt;andWhere($this-&gt;createQueryBuilder('a')-&gt;expr()-&gt;notIn('a.id', $query));
  
     return array($sorted);
  </code></pre>
  
  <p>Of course apartment has a collection of visits and visit as a field named "apartment" which is related to the apartment object. </p>
  
  <p>I really didn't find a proprer way to do it and I want to avoid doing SQL, to improve my understanding of Doctrine. </p>
  
  <p>Thank you for your help because I'm stuck right now :/</p>
  
  <p>EDIT 1: forgot to mention that I want to get apartments that don't have visits between required dates or that don't have any visits at all</p>
  
  <p>EDIT 2 : </p>
  
  <pre><code>public function findByCustom($parameters): ?array
  {
     $query =  $this-&gt;createQueryBuilder('a');
      $withoutInner = $this-&gt;createQueryBuilder("a");
  
     foreach ($parameters as $key=&gt; $parameter){
         if($key != 'keywords' and $key!= 'priceMin' &amp; $key!='priceMax' and $key!="typeAp" and $key!="searchVisitDate")
         {
             $withoutInner-&gt;orWhere('a.'.$key." = '".$parameter."'");
             $query-&gt;orWhere('a.'.$key." = '".$parameter."'");
         }
  
  
         if($key == "typeAp")
         {
             $typeApQuery = "";
             foreach ($parameters[$key] as $index =&gt; $type)
             {
                 if($index !== count($parameters[$key])-1)
                 {
                     $typeApQuery.=" a.typeAp = '".$type."' or";
                 }
                 else
                 {
                     $typeApQuery.= " a.typeAp = '".$type."'";
                 }
             }
             $withoutInner-&gt;andWhere($typeApQuery);
             $query-&gt;andWhere($typeApQuery);
         }
     }
  
     $query-&gt;andWhere('a.rentPrice &gt;='.$parameters['priceMin']." and a.rentPrice &lt;= ".$parameters['priceMax']);
     $withoutInner-&gt;andWhere('a.rentPrice &gt;='.$parameters['priceMin']." and a.rentPrice &lt;= ".$parameters['priceMax']);
     $query
         -&gt;join('App\Entity\Visit', 'v', Join::WITH, 'a.id = v.apartment')
         -&gt;where("v.date between '2020-03-15' and '2020-03-19'");
  
      $query-&gt;getQuery()-&gt;getResult();
      $sorted = $withoutInner-&gt;andWhere($this-&gt;createQueryBuilder('a')-&gt;expr()-&gt;notIn('a.id', $query));
  </code></pre>
  
  <p>With this function I get doctrine error : </p>
  
  <pre><code>Error: Method Doctrine\Common\Collections\ArrayCollection::__toString() must not throw an exception, caught ErrorException: Catchable Fatal Error: Object of class Doctrine\ORM\EntityManager could not be converted to string
  </code></pre>