PHP code example of creocoder / yii2-taggable

1. Go to this page and download the library: Download creocoder/yii2-taggable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

creocoder / yii2-taggable example snippets


$this->createTable('{{%post}}', [
    'id' => Schema::TYPE_PK,
    'title' => Schema::TYPE_STRING . ' NOT NULL',
    'body' => Schema::TYPE_TEXT . ' NOT NULL',
]);

$this->createTable('{{%tag}}', [
    'id' => Schema::TYPE_PK,
    'name' => Schema::TYPE_STRING . ' NOT NULL',
    'frequency' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
]);

$this->createTable('{{%post_tag_assn}}', [
    'post_id' => Schema::TYPE_INTEGER . ' NOT NULL',
    'tag_id' => Schema::TYPE_INTEGER . ' NOT NULL',
]);

$this->addPrimaryKey('', '{{%post_tag_assn}}', ['post_id', 'tag_id']);

use creocoder\taggable\TaggableBehavior;

/**
 * ...
 * @property string $tagValues
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'taggable' => [
                'class' => TaggableBehavior::className(),
                // 'tagValuesAsArray' => false,
                // 'tagRelation' => 'tags',
                // 'tagValueAttribute' => 'name',
                // 'tagFrequencyAttribute' => 'frequency',
            ],
        ];
    }

    public function rules()
    {
        return [
            //...
            ['tagValues', 'safe'],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_ALL,
        ];
    }

    public static function find()
    {
        return new PostQuery(get_called_class());
    }

    public function getTags()
    {
        return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
            ->viaTable('{{%post_tag_assn}}', ['post_id' => 'id']);
    }
}

use creocoder\taggable\TaggableQueryBehavior;

class PostQuery extends \yii\db\ActiveQuery
{
    public function behaviors()
    {
        return [
            TaggableQueryBehavior::className(),
        ];
    }
}

$post = new Post();

// through string
$post->tagValues = 'foo, bar, baz';

// through array
$post->tagValues = ['foo', 'bar', 'baz'];

$post = Post::findOne(1);

// through string
$post->addTagValues('bar, baz');

// through array
$post->addTagValues(['bar', 'baz']);

$post = Post::findOne(1);

// through string
$post->removeTagValues('bar, baz');

// through array
$post->removeTagValues(['bar', 'baz']);

$post = Post::findOne(1);
$post->removeAllTagValues();

$posts = Post::find()->with('tags')->all();

foreach ($posts as $post) {
    // as string
    $tagValues = $post->tagValues;

    // as array
    $tagValues = $post->getTagValues(true);
}

$post = Post::findOne(1);

// through string
$result = $post->hasTagValues('foo, bar');

// through array
$result = $post->hasTagValues(['foo', 'bar']);

// through string
$posts = Post::find()->anyTagValues('foo, bar')->all();

// through array
$posts = Post::find()->anyTagValues(['foo', 'bar'])->all();

// through string
$posts = Post::find()->anyTagValues('foo-slug, bar-slug', 'slug')->all();

// through array
$posts = Post::find()->anyTagValues(['foo-slug', 'bar-slug'], 'slug')->all();

// through string
$posts = Post::find()->allTagValues('foo, bar')->all();

// through array
$posts = Post::find()->allTagValues(['foo', 'bar'])->all();

// through string
$posts = Post::find()->allTagValues('foo-slug, bar-slug', 'slug')->all();

// through array
$posts = Post::find()->allTagValues(['foo-slug', 'bar-slug'], 'slug')->all();

// through string
$posts = Post::find()->relatedByTagValues('foo, bar')->all();

// through array
$posts = Post::find()->relatedByTagValues(['foo', 'bar'])->all();

// through string
$posts = Post::find()->relatedByTagValues('foo-slug, bar-slug', 'slug')->all();

// through array
$posts = Post::find()->relatedByTagValues(['foo-slug', 'bar-slug'], 'slug')->all();
bash
$ yii migrate/create create_post_tag_assn_table