Reputation: 398
Im creating form that inserts new set in db. And inserts it under the given parent id.
Im now in beforeSave()
where I want other db entries to be updated.
protected function beforeSave()
{
if (parent::beforeSave()){
if($this->parent_id >= 0){
$parent = self::model()->findByPk($this->parent_id);
if($parent){
self::model()->updateAll(); // cant figure out how to use this as I need.
}else{
return false;
}
}
}
}
I think update all would be appropriate function here, but I need it to update every db entry
update `nodes` where left > $parent->left to left = left + 2
update `nodes` where right > $parent->right to right = right + 2
Here is CActiveRecord updateAll() reference:
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#updateAll-detail
I want to do this not making many function calls. How can I do it?
Upvotes: 0
Views: 56
Reputation: 14459
You can do:
self::model()->updateAll(array(
'left'=>$this->left+2,
),'left>:pleft',array(":pleft"=>$parent->left));
And
self::model()->updateAll(array(
'right'=>$this->right+2
),'right>:pright',array(":pright"=>$parent->right));
If $this->right
and $this->left
are independent, you can use updateCounter()
method like below:
self::model()->updateCounters(
array('right'=>2), //increments +2
'right>:pright',array(":pright"=>$parent->right)
);
Above code, updates all rows(WHERE right > parent.right
) and increments all rights by 2.
Upvotes: 1