简体   繁体   English

在SonataAdmin类中分配克隆的对象

[英]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实体中,我有$tasksArrayCollection 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM