小编div*_*oZA的帖子

Symfony2/Doctrine:如何将OneToMany实体重新保存为级联新行

首先,这个问题类似于如何将实体重新保存为Doctrine 2中的另一行

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

我使用了一个非常简单的教室示例,其中包含许多学生,以保持简单.

所以我可能有Classroom = 1的ClassroomA,它有5个学生(ids 1到5).我想知道如何在Doctrine2中使用该实体并将其重新保存到数据库中(在潜在的数据更改之后)全部使用新ID并且原始行在持久化/刷新期间不受影响.

让我们首先定义我们的Doctrine实体.

课堂实体:

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 ============

}
Run Code Online (Sandbox Code Playgroud)

学生实体:

namespace Acme\TestBundle\Entity;

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

/**
 * …
Run Code Online (Sandbox Code Playgroud)

php doctrine clone symfony doctrine-orm

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

clone ×1

doctrine ×1

doctrine-orm ×1

php ×1

symfony ×1