creocoder\taggable\TaggableBehavior::afterSave PHP Method

afterSave() public method

public afterSave ( ) : void
return void
    public function afterSave()
    {
        if ($this->_tagValues === null) {
            return;
        }
        if (!$this->owner->getIsNewRecord()) {
            $this->beforeDelete();
        }
        $tagRelation = $this->owner->getRelation($this->tagRelation);
        $pivot = $tagRelation->via->from[0];
        /* @var ActiveRecord $class */
        $class = $tagRelation->modelClass;
        $rows = [];
        foreach ($this->_tagValues as $value) {
            /* @var ActiveRecord $tag */
            $tag = $class::findOne([$this->tagValueAttribute => $value]);
            if ($tag === null) {
                $tag = new $class();
                $tag->setAttribute($this->tagValueAttribute, $value);
            }
            if ($this->tagFrequencyAttribute !== false) {
                $frequency = $tag->getAttribute($this->tagFrequencyAttribute);
                $tag->setAttribute($this->tagFrequencyAttribute, ++$frequency);
            }
            if ($tag->save()) {
                $rows[] = [$this->owner->getPrimaryKey(), $tag->getPrimaryKey()];
            }
        }
        if (!empty($rows)) {
            $this->owner->getDb()->createCommand()->batchInsert($pivot, [key($tagRelation->via->link), current($tagRelation->link)], $rows)->execute();
        }
    }