3

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

我正在尝试:

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

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

$copy->save();

这段代码不起作用,但我认为它正在路上。

4

5 回答 5

5

我永远无法让深拷贝功能正常运行。

我为我的一个模型手动编写了一个深拷贝函数,就像这样

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();
}
于 2010-03-04T07:38:44.820 回答
3

你可以在copy() 这里阅读。它需要一个可选参数$deep

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

所以

$copy = $object->copy(true);

应该这样做。

于 2010-03-01T22:08:48.727 回答
2

对不起,如果我要复活这个线程......

我最近发现自己正在寻找一个解决方案,我需要复制记录并保留原始参考。深拷贝$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();
}

希望这可以帮助 :)

于 2010-04-18T20:33:30.527 回答
0

我正在使用 Symfony1.4.1 并且使用 Doctrine 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 有一个由多个对象“拥有”的元素(多对多,我拥有的其他特殊情况)时,我可以控制关​​系的哪一侧得到 pre(非惰性) 加载。

于 2010-07-06T23:08:49.867 回答
0

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

    $table = $entidade->getTable();
    $relations = $table->getRelations();
    foreach($relations as $relation => $data) {
        try {
            $entity->loadReference($relation);
        } catch(Exception $e) {
            die($e->getMessage());
        }
    }
于 2010-04-14T20:31:37.647 回答