复制具有所有关系的Doctrine对象 [英] Copy a Doctrine object with all relations

查看:129
本文介绍了复制具有所有关系的Doctrine对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试:

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

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

$ copy-> save();

此代码无效,但我认为它在路上。

解决方案

我从来不会让深层复制功能正常运行。



我手动编码这样一个我的模型的深度拷贝功能

  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 $ questions)
{
$ answers = $ question-> Survey_Answers;
$ newQuestion = $ question-> copy();
$ newQuestion-> survey_surveys_id = $ survey-> id;
$ newQuestion-> save();
$ newAnswers = new Doctrine_Collection(Survey_Model_Answer);

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


I want to copy a record with all his relations.

I'm trying with:

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

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

$copy->save();

This code doesn't works, but I think it's on the way.

解决方案

I never could get the deep copy function to operate correctly.

I manually coded a deep copy function for one of my models like this

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

这篇关于复制具有所有关系的Doctrine对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆