I read about behavoirs in yii and a question arose. If one method save () is used to update and insert a record in a table, can we write the behavoir for the update, not for the addition of the record? Or nothing?

    2 answers 2

    Found a solution in behavoir beforeSave to check isNewRecord. If false, an update has occurred. If there is a more elegant solution, offer.

    • All right More elegant nowhere. You can count yourself the answer :) - VasyOk
    • @VasyOk, reputation does not allow) - Ray

    Well, actually if you look at the code of the save method

    public function save($runValidation=true,$attributes=null) { if(!$runValidation || $this->validate($attributes)) return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes); else return false; } 

    then the insert or update method is essentially executed, and then put behavior on them

    • @Ale_x, I watched. But rewriting existing code on insert and update is no longer an option. Everywhere used save (). And afterUpdate () there is no such (( - Ray