我如何检查 Doctrine 引用是否有效?

How can I check if a Doctrine reference is valid?

我尝试获取文档的引用:

$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 = "";
}

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

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