2012-04-12 21 views
1

我試圖更新某個特定實體(主實體)的實體集合(子實體)的位置, 。一切工作,直到方法執行flush指令將更新推送到數據庫。Symfony 2:EntityManager :: flush消息錯誤「ArrayCollection :: __ construct()必須是一個數組,對象給出」

錯誤信息是:

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given, called in \vendor\doctrine\lib\Doctrine\ORM\UnitOfWork.php on line 426 and defined in \vendor\doctrine-common\lib\Doctrine\Common\Collections\ArrayCollection.php line 46  

控制器的方法是:

public function layoutAction($id, $entity, $subentity) 
{ 
    //-- Get Repository for master entity class 
    $object = $this->getRepository($entity)->find($id); 
    $em = $this->getDoctrine()->getEntityManager();  

    //-- Get form datas 
    $datas = $this->get('request')->get('items', array()); 

    //-- Update position for each msater entity childs of given subentity 
    $method = "get" . ucfirst($subentity) . "s";   
    foreach($object->$method() as $item){ 
     $item->setPosition(array_search($item->getId(), $datas)); 
     $em->persist($item);    
    } 

    //-- Push updates 
    $em->flush(); 

    //-- Notification + Redirection 
    ... 
} 

萬事達實體具有子實體和子實體一個一對多關係與主實體的一對一的關係。下面的示例聲明:

主實體:

<one-to-many field="subentities" target-entity="Subentity" mapped-by="masterentity" /> 

子實體:

<one-to-one field="masterentity" target-entity="MasterEntity" inversed-by="subentities"> 
    <join-column name="idMaster" referenced-column-name="idMaster" /> 
</one-to-one> 

我不明白爲什麼會觸發這個錯誤,但我相信它來自關係。

回答

0

您的地圖很混亂。如果你定義一個 Masterentity有許多子實體,然後Subentity::masterentity你應該設置反向關係:多到一個,而不是一個對一個。

+0

這確實是導致此錯誤的一對一關係。當我通過多對一替換一個並且我的錯誤消失並且更新成功時。謝謝Meze! – 2012-04-12 10:00:22

相關問題