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 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿