Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2:EntityManager::flush with message“上出现错误;ArrayCollection::uu construct()必须是数组,对象为“给定”;_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php Symfony 2:EntityManager::flush with message“上出现错误;ArrayCollection::uu construct()必须是数组,对象为“给定”;

Php Symfony 2:EntityManager::flush with message“上出现错误;ArrayCollection::uu construct()必须是数组,对象为“给定”;,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我正在尝试更新特定实体(主实体)的实体集合(子实体)的位置。在方法执行flush指令将更新推送到数据库之前,一切都正常 错误消息是: 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\Unit

我正在尝试更新特定实体(主实体)的实体集合(子实体)的位置。在方法执行flush指令将更新推送到数据库之前,一切都正常

错误消息是:

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>


我不明白为什么会触发此错误,但我相信它来自关系。

您的地图令人困惑。如果您定义了one主实体有many子实体,那么对于
Subentity::Masterentity
您应该设置反向关系:many to one,而不是一对一。

导致此错误的确实是一对一关系。当我将一对一替换为多对一时,错误消失,更新成功。谢谢你,梅兹!