【问题标题】:Copy a Doctrine object with all relations复制具有所有关系的 Doctrine 对象
【发布时间】:2010-03-01 16:59:19
【问题描述】:

我想复制他所有关系的记录。

我正在尝试:

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

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

$copy->save();

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

【问题讨论】:

    标签: php orm doctrine symfony1 symfony-1.4


    【解决方案1】:

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

    我为我的一个模型手动编写了一个深拷贝函数

    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();
    }
    

    【讨论】:

      【解决方案2】:

      您可以阅读有关copy() here 的信息。它需要一个可选参数$deep:

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

      所以

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

      应该这样做。

      【讨论】:

      • 我认为该参数无法正常工作。在我的模型中,我有 2 个行为(I18n 和 Sluggable)在一个嵌套集中工作。可能这就是 copy() 方法失败的原因。
      • 我查看了代码——“deep”参数仅在加载时复制引用。因此,您必须在克隆之前访问 $object 的所有引用,或者找到一种方法来急切地加载引用。
      【解决方案3】:

      抱歉,我要复活这个帖子...

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

      希望这会有所帮助:)

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          我正在使用 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(非懒惰) 已加载。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多