简体   繁体   English

symfony2 ContextErrorException:Catchable Fatal Error:类Proxies \\ __ CG __ \\ ... \\ Entity \\ ...的对象无法转换为字符串

[英]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

  • Category 类别
  • User 用户
  • Post 岗位

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 __... \\ BlogBu​​ndle \\ 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. 此外,由于CategoryUser对象被传递给category_iduser_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键指定我们要在表单构建器中返回哪个字段。

Add a __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;
}

OR add the property key to your form builder 或者将属性键添加到表单构建器

// 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 ,我只有字段idname - 所以唯一的字符串表示是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.

相关问题 可捕获的致命错误:无法将类Proxies \\ __ CG __ \\ AppBundle \\ Entity \\ Ticket的对象转换为字符串 - Catchable Fatal Error: Object of class Proxies\__CG__\AppBundle\Entity\Ticket could not be converted to string Symfony错误(“可捕获的致命错误:Proxies__CG __ \\ AppBundle \\ Entity \\ Type类的对象无法转换为字符串”) - Symfony error (“Catchable Fatal Error: Object of class Proxies__CG__\AppBundle\Entity\Type could not be converted to string”) Symfony2原则:ContextErrorException:可捕获的致命错误:DateTime类的对象无法转换为字符串 - Symfony2 Doctrine: ContextErrorException: Catchable Fatal Error: Object of class DateTime could not be converted to string Object 的 class Proxies\__CG__\App\Entity\... 无法转换为 int - Object of class Proxies\__CG__\App\Entity\... could not be converted to int symfony2 可捕获的致命错误:类的对象无法转换为字符串 - symfony2 Catchable Fatal Error: Object of class could not be converted to string Symfony2可捕获的致命错误:DateTime类的对象无法转换为字符串 - Symfony2 Catchable Fatal Error: Object of class DateTime could not be converted to string Symfony 2可捕获的致命错误:无法将类UserCategory的对象转换为字符串 - Symfony 2 Catchable Fatal Error: Object of class UserCategory could not be converted to string Symfony - Catchable Fatal Error:类App \\ Entity \\ Question的对象无法转换为字符串 - Symfony - Catchable Fatal Error: Object of class App\Entity\Question could not be converted to string Symfony可捕获的致命错误:无法将类AppBundle \\ Entity \\ Email的对象转换为字符串 - Symfony Catchable Fatal Error: Object of class AppBundle\Entity\Email could not be converted to string Symfony可捕获的致命错误:无法将类DataBundle \\ Entity \\ Location的对象转换为字符串 - Symfony Catchable Fatal Error: Object of class DataBundle\Entity\Location could not be converted to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM