title: ArrayCollection::matching with relations
body: <p>I have three doctrine2 models:</p>
  
  <pre><code>Person
   - $id
   - $schoolClasses (ArrayCollection, OneToMany PersonSchoolClass)
  
  SchoolClass
   - $id
  
  PersonSchoolClass (ManyToOne SchoolClass)
   - $id
   - $schoolClass
   - $person
   - $active
  </code></pre>
  
  <p>In my <code>Person::addSchoolClass(SchoolClass $schoolClass)</code>, I want to see, if there is already a <code>SchoolClass</code> in my <code>ArrayCollection&lt;PersonSchoolClass&gt;</code>.</p>
  
  <p>I use this code: </p>
  
  <pre><code>/**
   * @param SchoolClass $schoolClass
   * @return Person
   */
  public function addSchoolClass(SchoolClass $schoolClass)
  {
      $criteria = Criteria::create()
          -&gt;where(Criteria::expr()-&gt;eq("schoolClass.id", $schoolClass-&gt;getId()))
          -&gt;setFirstResult(0)
          -&gt;setMaxResults(1);
  
      $t = $this-&gt;getSchoolClasses()-&gt;matching($criteria);
  
      if (!$t) {
          $t = new PersonSchoolClass();
          $t-&gt;setPerson($this);
          $t-&gt;setSchoolClass($schoolClass);
          $this-&gt;getSchoolClasses()-&gt;add($schoolClass);
      }
  
      $t-&gt;setActive();
  
      return $this;
  }
  </code></pre>
  
  <p>But I always get a timeout at <code>$this-&gt;getSchoolClasses()-&gt;matching($criteria);</code> (process gets killed after a minute or so).</p>
  
  <p>Does anyone have an idea?</p>
  
  <p>Thank you very much!</p>