Symfony 从关联的实体获取值
我在实体“顾问”和“地位”之间有很多联系 其定义如下:Symfony 从关联的实体获取值,symfony,doctrine,event-listener,Symfony,Doctrine,Event Listener,我在实体“顾问”和“地位”之间有很多联系 其定义如下: @ORM\ManyToMany(targetEntity="Status", inversedBy="consultant") @ORM\JoinTable(name="consultant_status", joinColumns={ @ORM\JoinColumn(name="consultant_id", referencedColumnName="id") }, inverseJoinColumns={ @O
@ORM\ManyToMany(targetEntity="Status", inversedBy="consultant")
@ORM\JoinTable(name="consultant_status",
joinColumns={
@ORM\JoinColumn(name="consultant_id", referencedColumnName="id")
},
inverseJoinColumns={
@ORM\JoinColumn(name="status_id", referencedColumnName="id")
}
)
当我尝试(在更新事件上)通过以下方式从状态获取id时:
...
$entity = $args->getEntity();
if($entity instanceof Consultant){
$status_id= $entity->getStatu()->getId();
}
...
我得到:
调用未定义的方法条令\ORM\PersistentCollection:::getId()
有人知道我做错了什么吗?由于顾问和状态之间存在多个关系,
getStatus()
将返回一个集合
对象,其中包含与此顾问相关的所有状态
要想在你的整个身体中循环,只需使用foreach
foreach($entity->getStatu() as $statut) {
^^^
//you might have a typo here
$statut->getId();
//other stuff
}