ds261634878 2017-03-18 14:39
浏览 300
已采纳

Symfony 3 easyadmin __toString()不能抛出异常

I'm using EasyAdmin Bundle. When I'm trying to add a new element in Entity named "Company" which have 'ManyToMany' relation with "Service" entity I'm getting an error:

Error: Method AppBundle\Entity\Service::__toString() must not throw an exception

But when I'm going to add a new element in "Service" entity, everything works fine and the field with "Company" entities is displaying correctly.

I was trying to catch the exception implementing this workaround, but It doesn't take effect.

The Service class:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Company
 *
 * @ORM\Table(name="company")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
 */
class Company
{

/**
     * @var
     * 
     * Many Companys have Many Services.
     * @ORM\ManyToMany(targetEntity="Service", inversedBy="companys")
     * @ORM\JoinTable(name="companys_services")
     */
    private $services;

    public function __construct() {
        $this->services = new ArrayCollection();
    }
public function __toString() 
    {
        return $this->name;
    }
}

And the Service class:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Service
 *
 * @ORM\Table(name="service")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ServiceRepository")
 */
class Service
{
/**
     * Many Services have Many Companys.
     * @ORM\ManyToMany(targetEntity="Company", mappedBy="services")
     */
    private $companys;

    public function __construct() 
    {
        $this->companys = new ArrayCollection();
    }

    public function __toString() 
    {
        return (string) $this->name;
    }
}

What is wrong?

  • 写回答

1条回答 默认 最新

  • dsgsdg206050 2017-03-18 16:02
    关注

    The error message is so specific (Service::__toString() must not throw an exception) that the problem must be in the $this->name property of Service. Is it defined? Is a "normal" property or some advanced object which fails when casting it into a string?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真