addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { $this->rating = $this->getAvgRating(); $this->create_time = time(); } public function beforeUpdate() { $this->rating = $this->getAvgRating(); if ($this->deleted == 1) { $this->published = 0; } $this->update_time = time(); } protected function getAvgRating() { $sumRating = $this->rating1 + $this->rating2 + $this->rating3; return round($sumRating / 3, 2); } }