1

我正在使用 EasyAdmin 捆绑包。当我尝试在名为“Company”的实体中添加一个与“Service”实体具有“ManyToMany”关系的新元素时,出现错误:

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

但是当我要在“服务”实体中添加一个新元素时,一切正常,“公司”实体的字段显示正确。

我试图捕获实现解决方法的异常,但它没有生效。

服务类:

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;
    }
}

和服务类:

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;
    }
}

怎么了?

4

1 回答 1

0

错误消息非常具体 ( Service::__toString() must not throw an exception),因此问题一定出在 的$this->name属性中Service。定义了吗?是“普通”属性还是某些高级对象在将其转换为字符串时失败?

于 2017-03-18T16:02:58.370 回答