public function beforeDelete() { $pks = []; foreach ($this->owner->tags as $tag) { $pks[] = $tag->primaryKey; } if (count($pks)) { Tag::updateAllCounters(['frequency' => -1], ['in', 'id', $pks]); } Tag::deleteAll(['frequency' => 0]); TagAssign::deleteAll(['class' => get_class($this->owner), 'item_id' => $this->owner->primaryKey]); }