首先,这个问题类似于如何将实体重新保存为原则 2 中的另一行
不同之处在于,我尝试将数据保存在具有一对多关系的实体中。 我想将实体重新保存为父实体中的新行(在"一"端(,然后在每个后续子实体中(在"多"端(中另存为新行。
我用了一个非常简单的例子,一个教室有很多学生来保持简单。
所以我可能有 id=1 的 ClassroomA,它有 5 名学生(ids 1 到 5(。 我想知道如何在 Doctrine2 中获取该实体并将其重新保存到数据库中(在潜在的数据更改之后(,所有这些都带有新的 ID,并且在持久/刷新期间原始行保持不变。
让我们首先定义我们的教义实体。
课堂实体:
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 ============
}
以及我们的通用操作函数:
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 的父关系(在我们的示例中为"课堂"(,而子级数据(学生(则根据原始 ID 进行了更新。
提前感谢任何帮助。
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);
}
}
注意:在原则 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;
我这样做了,它工作正常。
在克隆的实体中,我们有神奇的__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();
我这样做:
if ($form->isValid()) {
foreach($classroom->getPupils() as $pupil) {
$pupil->setClassroom($classroom);
}
$em->persist($classroom);
$em->flush();
}