2017-03-18 9 views
1

私はEasyAdmin Bundleを使用しています。symfony 3 easyadmin __toString()は例外をスローしてはいけません。

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

をしかし、私は追加するつもりだとき:私は、私はエラーを取得しています「サービス」エンティティと「多対多」の関係を持っている「当社」という名前のエンティティの新しい要素を追加しようとしているとき"Service"エンティティの新しい要素は、すべて正常に動作し、 "Company"エンティティのフィールドは正しく表示されています。

this回避策を実装する例外をキャッチしようとしましたが、効果がありません。

サービスクラス:

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

何が悪いのでしょうか?

+1

サービスの正当な名前は存在しますか? – Federkun

+0

あなたは正確に何を意味するのか分かりません。私はSymfonyの初心者です。 – AppleandPear

答えて

0

エラーメッセージは、$this->nameプロパティの問題がServiceであるように特定されています(Service::__toString() must not throw an exception)。それは定義されていますか?文字列にキャストするときに失敗する「通常の」プロパティまたはいくつかの高度なオブジェクトですか?

+0

さて、あなたの答えは私を助けました。私は定義された変数 '$ serviceName'を持っていますが、' __toString() 'メソッドは' $ this-> name'でした。 '$ this-> serviceName'に変更して問題を解決しました。 – AppleandPear

関連する問題