Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 克隆原则实体与相关的一对多实体_Php_Symfony_Doctrine_Clone - Fatal编程技术网

Php 克隆原则实体与相关的一对多实体

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

我创建了一个名为Landing的实体,该实体与LandingContent具有OneToMany关系。一个平台可以有一个或多个内容

我需要克隆这个登陆实体,并在数据库中设置新的id。(工作正常)。我还需要用新id克隆登陆内容

着陆中的克隆方法是这样的:

 /**
     * 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);
        }
    }
}