Twig和Symfony2 - 实体没有找到 [英] Twig and Symfony2 - Entity was not found
问题描述
最后,我有一个像tat的对象:
paper.submission.authors
对于某些paper.submission,没有作者,在我的twig模板中,我在做:
{%for paper.ubmission.authors}
做某事
{%endfor%}
对于没有作者的paper.submission,我得到实体未找到异常。
是否有可能测试对象是否存在于我的for循环之前。
我尝试定义,总是真的然后,我已经尝试不是null,但这也是异常生成。
非常感谢你提前。
问题
当没有找到相关实体时,Doctrine会抛出此异常。这似乎是多余的,但事实上这是重要的。
这意味着它可以找到与之相关的ID,但是所做的请求原则与任何结果不匹配
我的猜测是,您的数据库表(实际上是链接表) submission.authors
包含的ID为$ code> 0 而不是 NULL
。
这样做,Doctrine认为有 IS ID为 0
的作者,因此找不到。
发生什么
submission.authors
始终存在。这是一个未初始化的原则代理。
var_dump($ submission-> getAuthors());
将显示您正在包含的内容 submission.authors
此时,不进行任何查询。它只是返回一个 PersistentCollection
带标志 isInitialized
为false。
当您尝试获取属性时,会发生异常
foreach($ submission-> getAuthors()as $ author){
}
执行此操作时,将检查 getAuthors
是否被初始化。如果没有,它将运行以下查询
SELECT< stuffs> FROM作者WHERE id = 0;
哪些没有匹配,会抛出一个 EntityNotFound
异常
修复
您必须将您的ID行的默认值设置为 NULL
并进行查询以更新所有 0
到 NULL
。
这样,您可以轻松测试 submission.authors
与不为空
教义不会运行任何查询如果它找到一个 NULL
I have an entity that is related to some other entities. On the end, I have an object like tat:
paper.submission.authors
For some of the paper.submission, there is no author, and in my twig template, I am doing:
{% 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.
Is thee any possibility to test if the object exists before my for loop.
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.
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 could find an ID related to it, but the request doctrine made didn't match any result.
My guess is that your database table (link table actually) submission.authors
contains IDs of 0
instead of NULL
.
With such, Doctrine thinks there IS an author with ID of 0
, and therefor, cannot find it.
What happens
submission.authors
always exists. It is an Uninitialized Doctrine Proxy.
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.
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
SELECT <stuffs> FROM authors WHERE id = 0;
Which returns no match and will throw an EntityNotFound
Exception
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
Doctrine will not run any query if it finds a NULL
这篇关于Twig和Symfony2 - 实体没有找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!