title: Symfony2 Doctrine Criteria
body: <p>I have an Entity called "Machine" with an association to Entity "Event"</p>
  
  <pre><code>/**
   * @var ArrayCollection
   *
   * @ORM\OneToMany(targetEntity="Event", mappedBy="machine", fetch="EXTRA_LAZY", cascade={"persist", "remove"})
   */
  private $events;
  </code></pre>
  
  <p>In my twig template I iterate over "Machine" entities</p>
  
  <pre><code>{% for machine in machines %}
      &lt;tr&gt;
          &lt;td&gt;{{ machine.name }}&lt;/td&gt;
          &lt;td&gt;{{ machine.lastEvent.value }}&lt;/td&gt;
      &lt;/tr&gt;
  {% endfor %}
  </code></pre>
  
  <p>I want to fetch last event for each machine</p>
  
  <pre><code>/**
   * Get machine current event value
   *
   * @return Event
   */
  public function getLastEvent()
  {
      $expression = Criteria::expr()
          -&gt;eq('machine', $this-&gt;id);
  
      $criteria = Criteria::create()
          -&gt;setMaxResults(1)
          -&gt;where($expression)
          -&gt;orderBy(['id' =&gt; 'DESC']);
  
      return $event = $this
          -&gt;getEvents()
          -&gt;matching($criteria)
          -&gt;first();
  }
  </code></pre>
  
  <p>When executing I get this error:</p>
  
  <p>"Cannot match on ApiBundle\Entity\Event::machine with a non-object value. Matching objects by id is not compatible with matching on an in-memory collection, which compares objects by reference."</p>
  
  <p>What I am doing wrong?</p>