Php 克隆原则实体与相关的一对多实体
我创建了一个名为Landing的实体,该实体与LandingContent具有OneToMany关系。一个平台可以有一个或多个内容 我需要克隆这个登陆实体,并在数据库中设置新的id。(工作正常)。我还需要用新id克隆登陆内容 着陆中的克隆方法是这样的:Php 克隆原则实体与相关的一对多实体,php,symfony,doctrine,clone,Php,Symfony,Doctrine,Clone,我创建了一个名为Landing的实体,该实体与LandingContent具有OneToMany关系。一个平台可以有一个或多个内容 我需要克隆这个登陆实体,并在数据库中设置新的id。(工作正常)。我还需要用新id克隆登陆内容 着陆中的克隆方法是这样的: /** * Clones the Landing */ public function __clone() { $this->id = null; $this->t
/**
* 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);
}
}
}
实际上,代码使用新的landingile在Landing表中创建一个新记录,并克隆内容,但在同一个Landing中,而不是在克隆的Landing中
如果有任何帮助,我们将不胜感激。(我也尝试在此处搜索其他问题以解决此问题)。您必须在内容中添加一个setter,以将其链接到新的登录:
/**
* 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);
}
}
}