【问题标题】:Symfony2/Doctrine: How to re-save an entity with a OneToMany as a cascading new rowSymfony2/Doctrine:如何使用 OneToMany 将实体重新保存为级联新行
【发布时间】:2012-02-23 16:58:03
【问题描述】:

首先,这个问题类似于How to re-save the entity as another row in Doctrine 2

不同之处在于我试图将数据保存在具有 OneToMany 关系的实体中。我想将实体重新保存为父实体中的新行(在“一”侧),然后作为新行在每个后续子实体中(在“多”侧)。

我使用了一个非常简单的示例,即有很多学生的教室来保持简单。

所以我可能有 id=1 的 ClassroomA,它有 5 个学生(id 1 到 5)。我想知道如何在 Doctrine2 中获取该实体并将其重新保存到数据库中(在可能的数据更改之后),所有这些都具有新的 ID,并且在持久/刷新期间原始行保持不变。

让我们首先定义我们的 Doctrine Entity。

教室实体:

namespace Acme\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="classroom")
 */
class Classroom
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $miscVars;  

   /**
     * @ORM\OneToMany(targetEntity="Pupil", mappedBy="classroom")
     */
    protected $pupils;

    public function __construct()
    {
        $this->pupils = new ArrayCollection();
    }       
    // ========== GENERATED GETTER/SETTER FUNCTIONS BELOW ============

}

学生实体:

namespace Acme\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="pupil")
 */
class Pupil
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $moreVars;

    /**
     * @ORM\ManyToOne(targetEntity="Classroom", inversedBy="pupils")
     * @ORM\JoinColumn(name="classroom_id", referencedColumnName="id")
     */
    protected $classroom;   

    // ========== GENERATED FUNCTIONS BELOW ============
}

还有我们的通用 Action 函数:

public function someAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $classroom = $em->find('AcmeTestBundle:Classroom', $id);

    $form = $this->createForm(new ClassroomType(), $classroom);

    if ('POST' === $request->getMethod()) {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // Normally you would do the following:
            $em->persist($classroom);
            $em->flush();

            // But how do I create a new row with a new ID 
            // Including new rows for the Many side of the relationship

            // ... other code goes here.
        }
    }

    return $this->render('AcmeTestBundle:Default:index.html.twig');
}

我尝试过使用克隆,但这只使用新 ID 保存了父关系(在我们的示例中为 Classroom),而子数据(学生)已根据原始 ID 进行更新。

提前感谢任何帮助。

【问题讨论】:

    标签: php doctrine symfony doctrine-orm clone


    【解决方案1】:

    clone 的意思是……

    当一个对象被克隆时,PHP 5 将对该对象的所有属性执行一个浅拷贝。任何引用其他变量的属性都将保持引用。

    如果您使用 Doctrine >= 2.0.2,您可以实现自己的自定义 __clone() 方法:

    public function __clone() {
        // Get current collection
        $pupils = $this->getPupils();
    
        $this->pupils = new ArrayCollection();
        foreach ($pupils as $pupil) {
            $clonePupil = clone $pupil;
            $this->pupils->add($clonePupil);
            $clonePupil->setClassroom($this);
        }
    }
    

    注意:在 Doctrine 2.0.2 之前,您不能在实体中实现 __clone() 方法,因为生成的代理类实现了自己的 __clone(),它不会检查或调用 parent::__clone()。因此,您必须为此创建一个单独的方法,例如clonePupils()(在Classroom),并在克隆实体后调用它。无论哪种方式,您都可以在 __clone()clonePupils() 方法中使用相同的代码。

    当你克隆你的父类时,这个函数也会创建一个包含子对象克隆的新集合。

    $cloneClassroom = clone $classroom;
    $cloneClassroom->clonePupils();
    
    $em->persist($cloneClassroom);
    $em->flush();
    

    您可能希望在您的 $pupils 集合上级联持久化以使持久化更容易,例如

    /**
     * @ORM\OneToMany(targetEntity="Pupil", mappedBy="classroom", cascade={"persist"})
     */
    protected $pupils;
    

    【讨论】:

    • 做到了,谢谢。我希望避免在函数中循环,但这最终是最简单的解决方案。我看到 Doctrine 的 EntityManager 类有一个“复制”功能,描述为“创建给定实体的副本。可以创建浅拷贝或深拷贝”。唯一的问题是该函数有一行抛出异常“未实现”。
    • 在当前的 Doctrine2 中仅供参考,您可以实现自己的 __clone,它会被调用。
    【解决方案2】:

    我这样做:

    if ($form->isValid()) {
        foreach($classroom->getPupils() as $pupil) {
            $pupil->setClassroom($classroom);
        }
        $em->persist($classroom);
        $em->flush();
    }
    

    【讨论】:

    • 这看起来不会创建新的 Pupil 实例;它将在两个教室之间共享每个学生。
    【解决方案3】:

    我就是这样做的,效果很好。

    在克隆实体内部,我们有 ma​​gic __clone()。我们也不会忘记我们的一对多

    /**
     * Clone element with values
     */
    public function __clone(){
        // we gonna clone existing element
        if($this->id){
            // get values (one-to-many)
            /** @var \Doctrine\Common\Collections\Collection $values */
            $values = $this->getElementValues();
            // reset id
            $this->id = null;
            // reset values
            $this->elementValues = new \Doctrine\Common\Collections\ArrayCollection();
            // if we had values
            if(!$values->isEmpty()){
                foreach ($values as $value) {
                    // clone it
                    $clonedValue = clone $value;
                    // add to collection
                    $this->addElementValues($clonedValue);
                }
            }
        }
    }
    /** 
     * addElementValues 
     *
     * @param \YourBundle\Entity\ElementValue $elementValue
     * @return Element
    */
    public function addElementValues(\YourBundle\Entity\ElementValue $elementValue)
    {
        if (!$this->getElementValues()->contains($elementValue))
        {
            $this->elementValues[] = $elementValue;
            $elementValue->setElement($this);
        }
    
        return $this;
    }
    

    在某个地方克隆它:

    // Returns \YourBundle\Entity\Element which we wants to clone
    $clonedEntity = clone $this->getElement();
    // Do this to say doctrine that we have new object
    $this->em->persist($clonedEntity);
    // flush it to base
    $this->em->flush();
    

    【讨论】:

      猜你喜欢
      • 2012-02-22
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多