首页 > 解决方案 > 如何检查 Doctrine 引用是否有效?

问题描述

我尝试获取文件的参考:

$result = $this->em->getReference('App\Entity\Documents', 522);

但我收到错误消息:

Entity of type 'App\Entity\Documents' for IDs id(522) was not found

有没有办法检查引用是否有效或初始化为真,或者具有特定 id 的文档是否存在?

就像是:

if($this->em->getReference('App\Entity\Documents', 522) == true){
 $result = $this->em->getReference('App\Entity\Documents', 522);
} else {
$result = "";
}

标签: phpdoctrine

解决方案


getReference()不会导致在数据库中查找。你可以使用find(),像这样

if($this->em->find('App\Entity\Documents', 522) !== null)

推荐阅读