我想用克隆的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
答案 0 :(得分: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;
}
}