2012-12-21 48 views
0

與集合場,其中收集包含實體領域(會員類型EID)形式,將收集的確認將導致實體領域引發以下錯誤:Symfony2的:收集驗證導致實體領域的錯誤

An exception has been thrown during the rendering of a template ("Notice: Object of class Mana\ClientBundle\Entity\Ethnicity could not be converted to int

未驗證,實體字段處理正確。並且將實體字段添加到驗證器不起作用。

收集:

->add('members', 'collection', array(
    'type' => new MemberType(), 
    'allow_add' => true, 
    'allow_delete' => true, 
    'by_reference' => false, 
    'prototype' => true, 
))   

會員類型:

->add('include', 'choice', array(
    'choices' => array('Yes' => 'Yes', 'No' => 'No'), 
    'empty_value' => false, 
    'required' => false)) 
->add('fname', null, array('required' => false)) 
->add('sname', null, array('required' => false)) 
->add('dob', 'dob_age') 
->add('gender', 'choice', array(
    'choices' => array('Male' => 'Male', 'Female' => 'Female'), 
    'empty_value' => false, 
    'required' => false)) 
->add('eid', 'entity', array(
    'property' => 'abbr', 
    'label' => '', 
    'class' => 'ManaClientBundle:Ethnicity', 
    'query_builder' => function(EntityRepository $er) { 
     return $er->createQueryBuilder('e') 
       ->orderBy('e.abbr', 'ASC'); 
    } 
)); 

驗證:

/** 
* @Assert\Collection(
* fields = { 
*  "fname" = @Assert\NotBlank(message = "Member first name may not be blank"), 
*  "dob" = @Assert\NotBlank(message = "Member DOB may not be blank"), 
*  }, 
*  allowExtraFields = true 
*) 
*/ 

會員單位:

class Member 
{ 

    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="hid", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Client",inversedBy="members",cascade={"remove", "persist"}) 
    * @ORM\JoinColumn(name="clientId", referencedColumnName="id") 
    * 
    */ 
    protected $client; 

    public function setClient(Client $client) 
    { 
     $this->client = $client; 
     return $this; 
    } 

    public function getClient() 
    { 
     return $this->client; 
    } 

    /** 
    * @var string $fname 
    * 
    * @ORM\Column(name="fname", type="string", length=30, nullable=false) 
    */ 
    private $fname; 

    /** 
    * @var string $sname 
    * 
    * @ORM\Column(name="sname", type="string", length=45, nullable=false) 
    */ 
    private $sname; 

    /** 
    * @var \DateTime $dob 
    * 
    * @ORM\Column(name="dob", type="date", nullable=true) 
    */ 
    private $dob; 

    /** 
    * @var string $include 
    * 
    * @ORM\Column(name="include", type="string", nullable=false) 
    */ 
    private $include; 

    /** 
    * @var string $gender 
    * 
    * @ORM\Column(name="gender", type="string", nullable=false) 
    */ 
    private $gender; 

    /** 
    * @var \DateTime $excludeDate 
    * 
    * @ORM\Column(name="exclude_date", type="date", nullable=true) 
    */ 
    private $excludeDate; 

    /** 
    * @var integer $eid 
    * 
    * @ORM\Column(name="eid", type="integer", nullable=true) 
    */ 
    private $eid; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() { 
     return $this->id; 
    } 

    /** 
    * Set fname 
    * 
    * @param string $fname 
    * @return member 
    */ 
    public function setFname($fname) { 
     $this->fname = $fname; 

     return $this; 
    } 

    /** 
    * Get fname 
    * 
    * @return string 
    */ 
    public function getFname() { 
     return $this->fname; 
    } 

    /** 
    * Set sname 
    * 
    * @param string $sname 
    * @return member 
    */ 
    public function setSname($sname) { 
     $this->sname = $sname; 

     return $this; 
    } 

    /** 
    * Get sname 
    * 
    * @return string 
    */ 
    public function getSname() { 
     return $this->sname; 
    } 

    /** 
    * Set dob 
    * 
    * @param \DateTime $dob 
    * @return member 
    */ 
    public function setDob($dob) { 
     $this->dob = $dob; 

     return $this; 
    } 

    /** 
    * Get dob 
    * 
    * @return \DateTime 
    */ 
    public function getDob() { 
     return $this->dob; 
    } 

    /** 
    * Set include 
    * 
    * @param string $include 
    * @return member 
    */ 
    public function setInclude($include) { 
     $this->include = $include; 

     return $this; 
    } 

    /** 
    * Get include 
    * 
    * @return string 
    */ 
    public function getInclude() { 
     return $this->include; 
    } 

    /** 
    * Set gender 
    * 
    * @param string $gender 
    * @return member 
    */ 
    public function setGender($gender) { 
     $this->gender = $gender; 

     return $this; 
    } 

    /** 
    * Get gender 
    * 
    * @return string 
    */ 
    public function getGender() { 
     return $this->gender; 
    } 

    /** 
    * Set excludeDate 
    * 
    * @param \DateTime $excludeDate 
    * @return member 
    */ 
    public function setExcludeDate($excludeDate) { 
     $this->excludeDate = $excludeDate; 

     return $this; 
    } 

    /** 
    * Get excludeDate 
    * 
    * @return \DateTime 
    */ 
    public function getExcludeDate() { 
     return $this->excludeDate; 
    } 

    /** 
    * Set eid 
    * 
    * @param integer $eid 
    * @return member 
    */ 
    public function setEid($eid) { 
     $this->eid = $eid; 

     return $this; 
    } 

    /** 
    * Get eid 
    * 
    * @return integer 
    */ 
    public function getEid() { 
     return $this->eid; 
    } 

種族實體:

class Ethnicity 
{ 
    public function __toString() { 
     return strval($this->id); 
    } 

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

    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string $description 
    * 
    * @ORM\Column(name="description", type="string", length=45, nullable=false) 
    */ 
    private $description; 

    /** 
    * @var string $abbr 
    * 
    * @ORM\Column(name="abbr", type="string", length=5, nullable=true) 
    */ 
    private $abbr; 



    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set description 
    * 
    * @param string $description 
    * @return Ethnicity 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 

     return $this; 
    } 

    /** 
    * Get description 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * Set abbr 
    * 
    * @param string $abbr 
    * @return Ethnicity 
    */ 
    public function setAbbr($abbr) 
    { 
     $this->abbr = $abbr; 

     return $this; 
    } 

    /** 
    * Get abbr 
    * 
    * @return string 
    */ 
    public function getAbbr() 
    { 
     return $this->abbr; 
    } 
} 

回答

0

原來我的錯誤是假設收集字段需要收集驗證器。相反,驗證者只需要在包含該集合的實體上。在這種情況下,我真正需要做的是在成員實體中指定驗證(並刪除客戶端實體中的註釋斷言。)當我簡化種族實體時,字段被正確處理。所以我的錯誤還包括了Symfony更復雜的概念 - 並非總是如此,有時只是真的!

0

請問您的會員單位有你的種族實體的關係?從「eid」這個名字來看,它看起來像是在試圖將一個種族類強制爲一個整數字段(「eid」)。

如果沒有看到兩個實體都很難找到任何東西。

+0

實體添加到上面。不確定添加關係的價值。沒有驗證時沒有必要。種族只是全球名稱和縮寫爲追蹤種族背景查找表。 – geoB

+0

我不知道Doctrine如何能夠看到關係,因爲你基本上是將集合添加到成員實體中。 您可以做的唯一事情就是將 '「property_path」=> false' 添加到實體字段,但您需要手動處理數據。 – Schwierig