I have a AR model that I am trying to duplicated but just need to manually change the foreign key.
我有一个AR模型,我试图复制,但只需要手动更改外键。
$_POST['competition_id'] = 99;
$prizes = CompetitionPrizes::model()->findAll('competition_id =:competition_id',array(':competition_id'=> $_POST['competition_id']));
This query basically queries the prizes table and gets all the rows for a particular competition. With the prizes object I would like to basically re-insert/duplicate the same information except the competition id which I want to manually set.
此查询基本上查询prizes表并获取特定竞争的所有行。对于奖品对象,我想基本上重新插入/复制相同的信息,除了我想手动设置的竞争ID。
I did something similar for an AR object that basically only has one row and that worked well, however in this instance as a competition can have more than one prize this same code won't.
我为AR对象做了类似的事情,基本上只有一行并且运行良好,但是在这种情况下,竞争可以有多个奖项,这个代码不会。
// My existing code for duplication process
$obj = Competitions::model()->find('competition_id=:competition_id', array(':competition_id' => $post['competition_id']));
$clone = clone $obj;
$clone->isNewRecord = true;
unset($clone->competition_id); // i want to remove this so it is auto inserted instead via the db
$clone->save();
This works great - how would I modify this on a 'collection' of prizes and have this duplicated into the database while setting my own 'competition_id' value.
这很有效 - 如何在奖品的'集合'上修改它,并在设置我自己的'competition_id'值时将其复制到数据库中。
Note - i'm to new to Yii, so please let me know if I have made any obvious errors/bad practice
注意 - 我是Yii的新手,所以如果我有任何明显的错误/不良做法,请告诉我
3 个解决方案
#1
18
Cloning won't work. You need to assign the attributes to a new object:
克隆不起作用。您需要将属性分配给新对象:
$obj = Competitions::model()->find('competition_id=:competition_id', array(':competition_id' => $post['competition_id']));
$clone = new Competitions;
$clone->attributes = $obj->attributes;
$clone->save();
#2
3
How about (yii2 syntax):
怎么样(yii2语法):
$model=Competitions::findOne([':competition_id' => $post['competition_id']]);
$model->id = null;
$model->isNewRecord = true;
$model->save();
#3
2
The answer for my problem although Michiel above helped me out - alternatively if you wouldn't mind adding another answer i'll give you the accepted answer.
我的问题的答案虽然Michiel上面帮助了我 - 或者如果你不介意添加另一个答案我会给你接受的答案。
foreach($models as $model)
{
$clone = new Competitions;
$clone->attributes = $model->attributes;
$clone->competition_id = '123' // custom var i am setting manually.
$clone->save();
}
#1
18
Cloning won't work. You need to assign the attributes to a new object:
克隆不起作用。您需要将属性分配给新对象:
$obj = Competitions::model()->find('competition_id=:competition_id', array(':competition_id' => $post['competition_id']));
$clone = new Competitions;
$clone->attributes = $obj->attributes;
$clone->save();
#2
3
How about (yii2 syntax):
怎么样(yii2语法):
$model=Competitions::findOne([':competition_id' => $post['competition_id']]);
$model->id = null;
$model->isNewRecord = true;
$model->save();
#3
2
The answer for my problem although Michiel above helped me out - alternatively if you wouldn't mind adding another answer i'll give you the accepted answer.
我的问题的答案虽然Michiel上面帮助了我 - 或者如果你不介意添加另一个答案我会给你接受的答案。
foreach($models as $model)
{
$clone = new Competitions;
$clone->attributes = $model->attributes;
$clone->competition_id = '123' // custom var i am setting manually.
$clone->save();
}