[英]symfony2 ContextErrorException: Catchable Fatal Error: Object of class Proxies\__CG__\…\Entity\… could not be converted to string
I have encountered something weird: 我遇到了一些奇怪的事:
I created three doctrine entities via app/console doctrine:generate:entity
: 我通过app/console doctrine:generate:entity
创建了三个doctrine实体app/console doctrine:generate:entity
:
I set up the relationships and everything works fine with the fixtures data ( app/console doctrine:fixtures:load
). 我建立了关系,一切都适用于灯具数据( app/console doctrine:fixtures:load
)。
A post belongs to a single category (category_id), and has one author (user_id). 帖子属于单个类别(category_id),并且有一个作者(user_id)。
I used app/console doctrine:generate:crud
to get CRUD operations for all my entities. 我使用app/console doctrine:generate:crud
来获取我所有实体的CRUD操作。
When I update a post, I get this strange error: 当我更新帖子时,我得到了这个奇怪的错误:
ContextErrorException: Catchable Fatal Error: Object of class Proxies__CG__...\\BlogBundle\\Entity\\Category could not be converted to string ContextErrorException:Catchable Fatal Error:类Proxies__CG __... \\ BlogBundle \\ Entity \\ Category的对象无法转换为字符串
In order to correctly display the dropdown fields I use in PostType()
: 为了正确显示我在PostType()
使用的下拉字段:
$builder ....
->add('categoryId','entity', array(
'class' => 'HotelBlogBundle:Category',
'property'=>'name'
))
->add('userId','entity',array(
'class'=>'UserBundle:User',
'property'=>'username'
))
Since I specify the property
option I don't need a __toString()
in my Entity classes. 由于我指定了property
选项,因此我的Entity类中不需要__toString()
。
If I create a __toString()
like this (both in Category & User Entities), the error is gone and works: 如果我像这样创建一个__toString()
(在类别和用户实体中),错误就会消失并且有效:
public function __toString()
{
return (string) $this->getId();
}
I am not sure if I do it the right way. 我不确定我是否以正确的方式做到了。
Also, since a Category
& User
object is passed to category_id
and user_id
fields, Doctrine (or Symfony) should be able to figure out the id column. 此外,由于Category
& User
对象被传递给category_id
和user_id
字段,因此Doctrine(或Symfony)应该能够找出id列。 What am I missing? 我错过了什么? Is there another way of doing this? 还有另一种方法吗?
I just ran into this issue with the mentioned error (and ended up here), I'll post the solution that worked for me - seeing as there are no other answers. 我刚刚提到了这个错误(并最终在这里),我将发布对我有用的解决方案 - 因为没有其他答案。
Like the OP, I also created a new table and join using Doctrine. 和OP一样,我也使用Doctrine创建了一个新表并加入。
This error is telling us that the joined object
cannot be converted to a string
. 此错误告诉我们无法将联接object
转换为string
。 We have to either provide a __toString()
method in the object so it can be converted to a string, or we can specify which field we want to return in our Form Builder using the property
key. 我们必须在对象中提供__toString()
方法,以便将其转换为字符串,或者我们可以使用property
键指定我们要在表单构建器中返回哪个字段。
__toString()
method to your object 向对象添加__toString()
方法 /**
* (Add this method into your class)
*
* @return string String representation of this class
*/
public function __toString()
{
return $this->name;
}
// where `name` equals your field name you want returned
$builder->add('category', null, ['property' => 'name'])
I simply added the __toString()
method to my entity
, I only have the fields id
and name
- so the only string representation is the name
. 我只是将__toString()
方法添加到我的entity
,我只有字段id
和name
- 所以唯一的字符串表示是name
。
In symfony 3, you can't use property. 在symfony 3中,您不能使用属性。
Here the error message : The option "property" does not exist. 这里出现错误消息:选项“属性”不存在。 ... ...
It's because the entity field doesn't use the option "property" anymore ( documentation ). 这是因为实体字段不再使用“属性”选项( 文档 )。
You should use the option "choice_label" instead. 您应该使用选项“choice_label”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.