在SonataAdmin类中分配克隆对象

时间:2013-06-26 13:28:47

标签: php symfony doctrine-orm sonata-admin

我想用克隆的Scenario对象创建Game对象。

Create Game form:
    Name: My game
    Scenario: MyScenario (Combo box)

根据Deep clone Doctrine entity with related entities问题的回答,我实施了__clone个方法。

我在__clone类的prePersist方法中使用GameAdmin方法。

public function prePersist($game)
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    $game->setAuthor($user);
    $cp = clone $game->getScenario(); //Error after add this
    $game->setScenario($cp);          //two lines
}

我不确定这是否适合进行此操作,因为我得到了MappingException

The class 'Doctrine\ORM\Persisters\ManyToManyPersister' was not found in the chain 
configured namespaces Sonata\MediaBundle\Entity, FOS\UserBundle\Entity, 
Sonata\UserBundle\Entity, Application\Sonata\MediaBundle\Entity, 
Application\Sonata\UserBundle\Entity, GM\AppBundle\Entity

1 个答案:

答案 0 :(得分:1)

Scenario实体中,我$tasksArrayCollection。我正在克隆整个集合,这会导致问题。

克隆循环中的每个task解决了问题:

public function __clone()
{
    if($this->id)
    {
        $this->setId(null);
        $ta = new ArrayCollection();
        foreach($this->tasks as $task)
        {
            $ta[] = clone $task;
        }
        $this->tasks = $ta;
    }
}