Php 复制具有所有关系的条令对象

Php 复制具有所有关系的条令对象,php,orm,doctrine,symfony1,symfony-1.4,Php,Orm,Doctrine,Symfony1,Symfony 1.4,我想复制一份他所有亲戚的记录 我正在尝试: $o = Doctrine::getTable('Table')->Find(x); $copy = $object->copy(); $relations = $o->getRelations(); foreach ($relations as $name => $relation) { $copy->$relation = $object->$relation->copy(); } $copy-

我想复制一份他所有亲戚的记录

我正在尝试:

$o = Doctrine::getTable('Table')->Find(x); 
$copy = $object->copy();
$relations = $o->getRelations();

foreach ($relations as $name => $relation) {
  $copy->$relation = $object->$relation->copy();
} 

$copy->save();

这段代码不起作用,但我认为它正在进行中。

您可以阅读有关
copy()
的内容。它采用可选参数
$deep

$deep
是否复制关系所针对的对象

所以


应该这样做。

我永远无法让深度复制功能正常运行

我为我的一个模型手动编写了一个深度复制函数,如下所示

public function copyAndSave ()
{
    $filters = array('id', 'created');

    $survey = $this->copy();

    $survey->Survey_Entries = new Doctrine_Collection("Survey_Model_Entry");
    $survey->Assignment_Assignments = new Doctrine_Collection("Assignment_Model_Assignment");
    $survey->Survey_Questions = new Doctrine_Collection("Survey_Model_Question");

    $survey->save();

    foreach ($this->Survey_Questions as $question)
    {
        $answers = $question->Survey_Answers;
        $newQuestion = $question->copy();
        $newQuestion->survey_surveys_id = $survey->id;
        $newQuestion->save();
        $newAnswers = new Doctrine_Collection("Survey_Model_Answer");

        foreach($answers as $answer)
        {
            $answer = $answer->copy();
            $answer->save();
            $answer->survey_questions_id = $newQuestion->id;
            $newAnswers->add($answer);
        }
        $newQuestion->Survey_Answers = $newAnswers;

        $survey->Survey_Questions->add($newQuestion);
    }
    return $survey->save();
}

我就是这样做的,但是需要一些修正

    $table = $entidade->getTable();
    $relations = $table->getRelations();
    foreach($relations as $relation => $data) {
        try {
            $entity->loadReference($relation);
        } catch(Exception $e) {
            die($e->getMessage());
        }
    }

抱歉,如果我要恢复此线程

最近我发现自己在寻找一个解决方案,需要复制一个记录并保留原始记录的引用。深度复制
$record->copy(true)
复制引用,这对我没有好处。这就是我的解决方案:

$record = Doctrine_Core::getTable('Foo')->find(1);
$copy = $record->copy();

foreach($record->getTable()->getRelations() as $relation) {
    if ($relation instanceof Doctrine_Relation_Association) {
        $ids = array();

        foreach ($relation->fetchRelatedFor($record) as $r) {    
            $ids[] = $r->getId();
        }

        $copy->link($relation->getAlias(), $ids);
    }
}

if ($copy->isValid()) {
    $copy->save();
}

希望这能有所帮助:)

我使用的是Symfony1.4.1,而它使用的是教义1.2.1(我认为)

当我发现一个已经存在的函数时,我一直在尝试创建一个能够完成上述所有功能的函数

在任何函数中尝试此操作,并查看结果:

  $tmp=$this->toArray(TRUE);
  var_dump($tmp);
  $this->refreshRelated();
  $tmp=$this->toArray();
  var_dump($tmp);
  $tmp=$this->toArray(TRUE);
  var_dump($tmp);
  exit();
我将尝试两种不同的方法:

A/将$this->refreshRelated()放入所有模型对象的构造函数中。
B/编写一个函数,该函数接受一个数组,该数组描述我想要填充的对象图。调用函数refereshRelatedGraph($objectGraphArray)。有了正确的数组结构(在每一级都有所有适当的关系名),我可以控制哪些关系被填充,哪些不被填充。这样做的一个用途是只填充子关系,而不是父关系。另一个是当一个ERD/Schema/ObjectGraph有一个由多个对象“拥有”的元素(多对多,我有其他特殊情况)时,我可以控制预加载(非惰性)关系的哪一侧。

我认为该参数不能正确工作。在我的模型中,我有两种行为(I18n和Slaggable)在嵌套集中工作。可能这就是copy()方法失败的原因。我看了一下代码,“deep”参数仅在加载引用时复制引用。因此,在克隆之前,您必须访问$object的所有引用,或者找到一种方法急切地加载引用。
  $tmp=$this->toArray(TRUE);
  var_dump($tmp);
  $this->refreshRelated();
  $tmp=$this->toArray();
  var_dump($tmp);
  $tmp=$this->toArray(TRUE);
  var_dump($tmp);
  exit();