0

我想用克隆的场景对象创建游戏对象。

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

基于对带有相关实体问题的深度克隆学说实体的回答,我已经实现了__clone方法。

我在课堂上的方法中使用__clone方法。prePersistGameAdmin

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
4

1 回答 1

1

Scenario我拥有的实体$tasks中是ArrayCollection. 我正在克隆整个集合,这会导致问题。

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

public function __clone()
{
    if($this->id)
    {
        $this->setId(null);
        $ta = new ArrayCollection();
        foreach($this->tasks as $task)
        {
            $ta[] = clone $task;
        }
        $this->tasks = $ta;
    }
}
于 2013-06-26T16:04:46.140 回答