title: Extract IDs from ArrayCollection to compare to array of IDs
body: <p>Good day,</p>
  
  <p>I have a class <strong>Cars</strong> and class <strong>CarTypes</strong> in a Many-To-Many relation, <strong>Cars</strong> contain an <strong>ArrayCollection</strong> of <strong>CarTypes</strong>. I want to search for Cars satisfying any of multiple selected <strong>CarTypes</strong> (passed from form in array). </p>
  
  <p>Till now I can only think of three nested loops, 1st over Cars, 2nd inner loop over <strong>id</strong>s array (from form) and 3rd inner loop on <strong>ArrayCollection</strong> of <strong>CarTypes</strong> in <strong>Cars</strong> to check if value from 2nd loop exists in 3rd.</p>
  
  <p>I have already checked below links related to closure. But these or none of the links in docs provide sufficient details on how to match an array of selected <strong>id</strong>s against <strong>id</strong>s from <strong>ArrayCollection</strong>.</p>
  
  <p><a href="https://stackoverflow.com/questions/8334356/doctrine-2-arraycollection-filter-method">Doctrine 2 ArrayCollection filter method</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7064528/doctrine-2-query-inside-entities">Doctrine 2, query inside entities</a></p>
  
  <p>I have Custom Repositories but feel it better to implement this in <strong>Cars</strong> class using <strong>exists</strong> of <strong>ArrayCollection</strong>, maybe something like following:</p>
  
  <pre><code>public function existsCarTypes($ctArray)
  {
      $CarTypes = $this-&gt;CarTypes;
      return $CarTypes-&gt;exists( 
              function($CarTypes) use ($ctArray) 
              {
                  if (in_array($ctArray , $CarTypes-&gt;getId() )) 
                  {
                      return true;
                  }
                  return false;
              }
          );
  }
  </code></pre>
  
  <p>But this is not working, Is there a better way to do this, please provide any good documentation links.</p>