[英]Assigning cloned object in SonataAdmin class
I want to create Game object with cloned Scenario object. 我想用克隆的场景对象创建游戏对象。
Create Game form:
Name: My game
Scenario: MyScenario (Combo box)
Basing on answer for Deep clone Doctrine entity with related entities question I have implemented __clone
methods. 基于对具有相关实体问题的深层克隆主义实体的回答,我已经实现了
__clone
方法。
I'm using __clone
method in prePersist
method in GameAdmin
class. 我在
GameAdmin
类的prePersist
方法中使用__clone
方法。
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
}
I'm not sure is this a proper place for doing this operation because I'm getting MappingException
: 我不确定这是执行此操作的合适位置,因为我正在获取
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
In Scenario
entity I have $tasks
which is ArrayCollection
. 在
Scenario
实体中,我有$tasks
是ArrayCollection
。 I was cloning entire collection and that cause problems. 我正在克隆整个集合,这会导致问题。
Cloning each task
in loop solves problem: 循环克隆每个
task
可以解决问题:
public function __clone()
{
if($this->id)
{
$this->setId(null);
$ta = new ArrayCollection();
foreach($this->tasks as $task)
{
$ta[] = clone $task;
}
$this->tasks = $ta;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.