src/TaggableBehavior.php
public function afterSave()
    {
        if ($this->_tagsList === null) {
            return;
        }
        $relation = $this->owner->getRelation('tagsList', false);
        if (!($relation instanceof ActiveQuery)) {
            return;
        }
        if (!$this->owner->getIsNewRecord()) {
            // clear old tags
            $this->beforeDelete();
            $this->afterDelete();
        }
        /** @var ActiveRecord $relationClass */
        $relationClass = $relation->modelClass;
        $ownerTagsList = [];
        foreach ($this->_tagsList as $tagText) {
            /* @var ActiveRecord $tag */
            $tag = $relationClass::findOne(['text' => $tagText]);
            if ($tag === null) {
                $tag = new $relationClass();
                $tag->setAttribute('text', $tagText);
            }
            $tag->setAttribute('count', $tag->getAttribute('count') + 1);
            if ($tag->save()) {
                $ownerTagsList[] = [$this->owner->getPrimaryKey(), $tag->getPrimaryKey()];
            }
        }
        if (!empty($ownerTagsList)) {
            $this->owner->getDb()
                ->createCommand()
                ->batchInsert($relation->via->from[0], [key($relation->via->link), current($relation->link)], $ownerTagsList)
                ->execute();
        }
    }
public function afterSave()
    {
        if ($this->_tagsList === null) {
            return;
        }
        $relation = $this->owner->getRelation('tagsList', false);
        if (!($relation instanceof ActiveQuery)) {
            return;
        }
        if (!$this->owner->getIsNewRecord()) {
            // clear old tags
            $this->beforeDelete();
            $this->afterDelete();
        }
        /** @var ActiveRecord $relationClass */
        $relationClass = $relation->modelClass;
        $ownerTagsList = [];
        foreach ($this->_tagsList as $tagText) {
            /* @var ActiveRecord $tag */
            $tag = $relationClass::findOne(['text' => $tagText]);
            if ($tag === null) {
                $tag = new $relationClass();
                $tag->setAttribute('text', $tagText);
            }
            $tag->setAttribute('count', $tag->getAttribute('count') + 1);
            if ($tag->save()) {
                $ownerTagsList[] = [$this->owner->getPrimaryKey(), $tag->getPrimaryKey()];
            }
        }
        if (!empty($ownerTagsList)) {
            $this->owner->getDb()
                ->createCommand()
                ->batchInsert($relation->via->from[0], [key($relation->via->link), current($relation->link)], $ownerTagsList)
                ->execute();
        }
    }
public function afterSave ( ) { if ( $ this -> _tagsList === null ) { return ; } $ relation = $ this -> owner -> getRelation ( 'tagsList' , false ) ; if ( ! ( $ relation instanceof ActiveQuery ) ) { return ; } if ( ! $ this -> owner -> getIsNewRecord ( ) ) { // clear old tags $ this -> beforeDelete ( ) ; $ this -> afterDelete ( ) ; } /** @var ActiveRecord $relationClass */ $ relationClass = $ relation -> modelClass ; $ ownerTagsList = [ ] ; foreach ( $ this -> _tagsList as $ tagText ) { /* @var ActiveRecord $tag */ $ tag = $ relationClass :: findOne ( [ 'text' => $ tagText ] ) ; if ( $ tag === null ) { $ tag = new $ relationClass ( ) ; $ tag -> setAttribute ( 'text' , $ tagText ) ; } $ tag -> setAttribute ( 'count' , $ tag -> getAttribute ( 'count' ) + 1 ) ; if ( $ tag -> save ( ) ) { $ ownerTagsList [ ] = [ $ this -> owner -> getPrimaryKey ( ) , $ tag -> getPrimaryKey ( ) ] ; } } if ( ! empty ( $ ownerTagsList ) ) { $ this -> owner -> getDb ( ) -> createCommand ( ) -> batchInsert ( $ relation -> via -> from [ 0 ] , [ key ( $ relation -> via -> link ) , current ( $ relation -> link ) ] , $ ownerTagsList ) -> execute ( ) ; } }
After owner save action
After owner save action