Symfony2 doctrine selection criteria
I have an Entity called "Machine" with an association with Entity "Event"
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Event", mappedBy="machine", fetch="EXTRA_LAZY", cascade={"persist", "remove"})
*/
private $events;
In my twig template I am iterating over the "Machine" items
{% for machine in machines %}
<tr>
<td>{{ machine.name }}</td>
<td>{{ machine.lastEvent.value }}</td>
</tr>
{% endfor %}
I want to get the latest event for each car
/**
* Get machine current event value
*
* @return Event
*/
public function getLastEvent()
{
$expression = Criteria::expr()
->eq('machine', $this->id);
$criteria = Criteria::create()
->setMaxResults(1)
->where($expression)
->orderBy(['id' => 'DESC']);
return $event = $this
->getEvents()
->matching($criteria)
->first();
}
When running this error, I am getting the following error:
"Cannot match on ApiBundle \ Entity \ Event :: machine with a non-object value. Object matching by id is incompatible with matching against an in-memory collection that compares objects by reference.
What am I doing wrong?
+3
source to share