我创建了一个名为Landing的实体,与LandingContent的OneToMany关系。 One Landing可以有一个或多个内容。
我需要克隆此Landing实体并使用新id在数据库中设置。 (这很好)。此外,我还需要使用新的id克隆LandingContent。
Landing中的克隆方法就是这样:
/**
* Clones the Landing
*/
public function __clone()
{
$this->id = null;
$this->title = new LandingTitle('Copia de ' . $this->getTitle()->getValue());
$contents = $this->getContents();
$this->contents = new ArrayCollection();
if(count($contents) > 0){
foreach ($contents as $content) {
$cloneContent = clone $content;
$this->contents->add($cloneContent);
}
}
}
实际上,代码使用新的Landingitle在Landing表中创建一条新记录,并在同一个Landing中克隆内容,而不是在克隆的Landing中。
任何帮助将不胜感激。(我也试图通过搜索其他问题来解决问题。)
答案 0 :(得分:0)
您必须在内容中添加一个setter才能将它们链接到新的Landing:
/**
* Clones the Landing
*/
public function __clone()
{
$this->id = null;
$this->title = new LandingTitle('Copia de ' . $this->getTitle()->getValue());
$contents = $this->getContents();
$this->contents = new ArrayCollection();
if(count($contents) > 0){
foreach ($contents as $content) {
$cloneContent = clone $content;
$cloneContent->setLanding($this);
$this->contents->add($cloneContent);
}
}
}