2012-04-12 20 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

相关问题