ant*_*kas 3 symfony doctrine-orm
我有一个名为"机器"的实体,与实体"事件"有关联
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Event", mappedBy="machine", fetch="EXTRA_LAZY", cascade={"persist", "remove"})
*/
private $events;
Run Code Online (Sandbox Code Playgroud)
在我的twig模板中,我迭代"机器"实体
{% for machine in machines %}
<tr>
<td>{{ machine.name }}</td>
<td>{{ machine.lastEvent.value }}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我想为每台机器获取最后一个事件
/**
* 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();
}
Run Code Online (Sandbox Code Playgroud)
执行时我收到此错误:
"无法在具有非对象值的ApiBundle\Entity\Event :: machine上匹配.通过id匹配对象与内存集合上的匹配不兼容,内存集合通过引用比较对象."
我做错了什么?
你必须更换
$expression = Criteria::expr()
->eq('machine', $this->id);
Run Code Online (Sandbox Code Playgroud)
同
$expression = Criteria::expr()
->eq('machine', $this);
Run Code Online (Sandbox Code Playgroud)
因为machine是一个关系,而且Doctrine需要使用实体而不是它的ID.
| 归档时间: |
|
| 查看次数: |
2290 次 |
| 最近记录: |