Trait luya\admin\traits\TaggableTrait

Implemented byluya\admin\models\StorageFile, luya\cms\models\Nav
Available since version1.2.2

Tag Trait.

This trait can be assigned in order to read the tag data for an ActiveRecord model.

See {{luya\admin\aws\TaggableActiveWindow}} order to atach the TagActiveWindow.

When the TagsTrait is attached to an {{luya\admin\ngrest\base\NgRestModel}} use the trait as below:

$tags = Model::findOne(1)->tags;

Or you can also get all tags assigned for this table:

$allTags = Model::findTags();

Since getTags() is a relation you can also preload those tag informations within a model like:

foreach (News::find()->with(['tags'])->all() as $news) {
    var_dump($news->tags);
}

To get all records with one or more tags matching: `php $matchTags = [1, 2, 3]; Model::find()

->joinWith(['tags'])
->andWhere(['in', 'tag_id', $matchTags])
->all();

Public Methods

Hide inherited methods

Method Description Defined By
cleanBaseTableName() Remove brackes from the table name, this makes sure the table name also works if the db prefix changes. luya\admin\traits\TaggableTrait
findTags() Get all tags associated with this table. luya\admin\traits\TaggableTrait
getTags() Returns all related tag for the current active record item. luya\admin\traits\TaggableTrait

Method Details

Hide inherited methods

cleanBaseTableName() public static method (available since version 2.0.0)

Remove brackes from the table name, this makes sure the table name also works if the db prefix changes.

public static string cleanBaseTableName ( $tableName )
$tableName

                public static function cleanBaseTableName($tableName)
{
    return str_replace(['{{%', '}}'], '', $tableName);
}

            
findTags() public static method

Get all tags associated with this table.

public static luya\admin\models\Tag findTags ( )
return luya\admin\models\Tag

An active record array from tag model.

                public static function findTags()
{
    return Tag::findRelationsTable(static::cleanBaseTableName(static::tableName()));
}

            
getTags() public method

Returns all related tag for the current active record item.

public luya\admin\models\Tag getTags ( )
return luya\admin\models\Tag

An active record array from tag models

                public function getTags()
{
    return $this->hasMany(Tag::class, ['id' => 'tag_id'])->viaTable('{{%admin_tag_relation}}', ['pk_id' => 'id'], function ($query) {
        $query->onCondition(['table_name' => static::cleanBaseTableName(static::tableName())]);
    });
}