php - 如何检查 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 = "";
}
解决方案
getReference()
不会导致在数据库中查找。你可以使用find()
,像这样
if($this->em->find('App\Entity\Documents', 522) !== null)
推荐阅读
- node.js - 为什么函数没有按预期返回值?
- python - 如何在一个变量中同时在python中以读取和追加模式打开文件
- java - 如何使用java从文本文件中删除重复的单词
- python - 您将如何解释以下内容:Python 中的 [position*-1]?* 让我失望
- qt-creator - 如何从侧边栏中删除项目?
- node.js - 来自不同 Web 服务的嵌套 axios 请求
- multithreading - 在子进程中运行的线程没有按照设置的警报时间超时
- cucumber-java - PIT-Cucumber 插件未在功能文件中找到场景
- node.js - 如何使用 Electron 和 React 显示从文件中读取的数据?
- reactjs - 当我更新浏览器时,React-router 渲染组件