Twig和Symfony2 - 未找到实体

时间:2022-09-24 06:47:17

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.

我已经尝试了定义,它总是如此。然后,我尝试过不是null,但这也是生成异常。

Thank you very much in advance.

非常感谢你提前。

3 个解决方案

#1


26  

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.

当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 IS an author with ID of 0, and therefor, cannot find it.

我的猜测是你的数据库表(实际上是链接表)submission.authors包含0而不是NULL的ID。有了这样,Doctrine认为有一个ID为0的作者,因此无法找到它。

What happens

submission.authors always exists. It is an Uninitialized Doctrine 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此时,不会进行任何查询。它只返回一个带有标志isInitialized为false的PersistentCollection。

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是非null

Doctrine will not run any query if it finds a NULL

如果Doctrine发现NULL,则不会运行任何查询

#2


5  

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#L160 but 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

在抛出新的EntityNotFoundException()之前,将以下代码放入ProxyFactory类; 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();

#3


4  

In your entity you can made something like this:

在您的实体中,您可以制作以下内容:

public function getSubmission(){
    if($this->Submission->getId()==0) return null;
    return $this->Submission;
}

#1


26  

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.

当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 IS an author with ID of 0, and therefor, cannot find it.

我的猜测是你的数据库表(实际上是链接表)submission.authors包含0而不是NULL的ID。有了这样,Doctrine认为有一个ID为0的作者,因此无法找到它。

What happens

submission.authors always exists. It is an Uninitialized Doctrine 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此时,不会进行任何查询。它只返回一个带有标志isInitialized为false的PersistentCollection。

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是非null

Doctrine will not run any query if it finds a NULL

如果Doctrine发现NULL,则不会运行任何查询

#2


5  

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#L160 but 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

在抛出新的EntityNotFoundException()之前,将以下代码放入ProxyFactory类; 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();

#3


4  

In your entity you can made something like this:

在您的实体中,您可以制作以下内容:

public function getSubmission(){
    if($this->Submission->getId()==0) return null;
    return $this->Submission;
}