I have an AR model that I am trying to duplicate, but just need to manually change the foreign key.
$_POST['competition_id'] = 99; $prizes = CompetitionPrizes::model()->findAll('competition_id =:competition_id',array(':competition_id'=> $_POST['competition_id']));
This query basically queries the prize table and retrieves all rows for a specific competition. With the prize object, I would basically reinsert / duplicate the same information except the contest identifier that I want to set manually.
I did something similar for an AR object that basically only has one line, and it worked fine, however, in this case, since a competitor can have more than one prize, the same code will not.
// 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 can I change this in the "collection" of prizes and duplicate it in the database when setting my own value "contest_id".
Note. I will be familiar with Yii, so please let me know if I made any obvious mistakes / bad practice.
source share