Nested set and simple way to insert record and update existing ones

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?

Are you using Yii2? You provided a link to a Yii1 class…

If your using Yii2 maybe you need updateAllCounters. In my case I have uesd it this way:




            self::updateAllCounters(['sort' => -1], ['AND',

                ['item_definition_id' => $this->item_definition_id],

                ['property_group_id' => $this->property_group_id],

                ['>', 'sort', $this->sort]]);



You can determine an increment value, also a negative one.

But be careful with beforeSave: If you just change a property like ‘name’ or so beforeSave will also be executed when your call save(). I had a similar functionality too, but then removed it and made it more explicit. Otherwise I had to distinguish what has changed and should counters be updated or not. Was too complicated for me.