php Twig 和 Symfony2 - 未找到实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17338105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Twig and Symfony2 - Entity was not found
提问by Milo?
I have an entity that is related to some other entities. On the end, I have an object like tat:
我有一个与其他一些实体相关的实体。最后,我有一个像 tat 这样的对象:
paper.submission.authors
For some of the paper.submission, there is no author, and in my twig template, I am doing:
对于一些paper.submission,没有作者,在我的twig模板中,我在做:
{% for author in paper.submission.authors}
do something
{% endfor %}
And for the paper.submission with no authors, I am getting "Entity was not found" exception.
对于没有作者的 paper.submission,我收到“找不到实体”异常。
Is thee any possibility to test if the object exists before my for loop.
你有没有可能在我的 for 循环之前测试对象是否存在。
I have try the is defined, it is always true. Then, I have tryed is not null, but this is also generating the exception.
我尝试过定义,它总是正确的。然后,我尝试过不为空,但这也会产生异常。
Thank you very much in advance.
非常感谢您提前。
回答by Touki
Problem
问题
Doctrine throws this Exception when it doesn't find the related entity. It seems redundant to say this, but in fact this is important.
It means it couldfind an ID related to it, but the request doctrine made didn't match any result.
Doctrine 在找不到相关实体时抛出此异常。这样说似乎多余,但实际上这很重要。
这意味着它可以找到与之相关的 ID,但提出的请求原则与任何结果都不匹配。
My guess is that your database table (link table actually) submission.authors
contains IDs of 0
instead of NULL
.
With such, Doctrine thinks there ISan author with ID of 0
, and therefor, cannot find it.
我的猜测是您的数据库表(实际上是链接表)submission.authors
包含的 ID0
而不是NULL
.
有了这样,学说认为有IS使用的ID的作者0
,以及为此,找不到它。
What happens
发生什么了
submission.authors
always exists. It is an UninitializedDoctrine Proxy.
submission.authors
永远存在。它是一个未初始化的Doctrine 代理。
var_dump($submission->getAuthors());
Would show you what contains exactly submission.authors
At this point, no queries are made. It simply returns a PersistentCollection
with a flag isInitialized
to false.
submission.authors
将向您显示确切包含的内容此时,没有进行任何查询。它只是返回PersistentCollection
带有标志isInitialized
为 false 的 。
The exception occurs when you're trying to get a property out of it
当您尝试从中获取属性时会发生异常
foreach ($submission->getAuthors() as $author) {
}
When doing this doctrine will check if getAuthors
is initialized. If not, it will run the following query
执行此原则时将检查是否getAuthors
已初始化。如果没有,它将运行以下查询
SELECT <stuffs> FROM authors WHERE id = 0;
Which returns no match and will throw an EntityNotFound
Exception
不返回匹配项并抛出EntityNotFound
异常
Fix
使固定
You must set your id row's default to NULL
and make a query to update all 0
's to NULL
.
With this, you can easily test submission.authors
with is not null
您必须将 id 行的默认值设置为NULL
并进行查询以将所有更新0
为NULL
。
有了这个,你可以轻松地测试submission.authors
与is not null
Doctrine will not run any query if it finds a NULL
如果 Doctrine 找到了,则不会运行任何查询 NULL
回答by Farid Movsumov
How to debug to find which related entity was not found?
如何调试以查找未找到哪个相关实体?
Exception message improved in repository https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L160but if you use older version you can do the following debugging.
改进了存储库https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Proxy/ProxyFactory.php#L160 中的异常消息, 但如果您使用旧版本,则可以进行以下调试。
If you use older version
如果您使用旧版本
Put following code to ProxyFactory class before throw new EntityNotFoundException();
line vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php:177
将以下代码放在 ProxyFactory 类之前throw new EntityNotFoundException();
line vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php:177
$entity = $classMetadata->getReflectionClass()->getShortName();
$id = $classMetadata->getIdentifierValues($proxy)['id'];
var_dump("$entity WHERE id = $id NOT FOUND.");exit;
throw new EntityNotFoundException();
回答by Jose Miguel Vidagany
In your entity you can made something like this:
在你的实体中,你可以做这样的事情:
public function getSubmission(){
if($this->Submission->getId()==0) return null;
return $this->Submission;
}