oks / yii2-tags
Tags
dev-master
2018-10-13 08:02 UTC
Requires
- yiisoft/yii2: ~2.0.0
This package is not auto-updated.
Last update: 2025-03-10 15:56:13 UTC
README
Tags
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist oks/yii2-tags "*"
or add
"oks/yii2-tags": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by :
You need to connect i18n for translations
'oks-tags' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@vendor/oks/yii2-tags/src/messages',
'sourceLanguage' => 'en',
'fileMap' => [
'oks-tags' => 'main.php',
],
],
and migrate the database
yii migrate --migrationPath=@vendor/oks/yii2-tags/src/migrations
Update (Active Record) - Single
example with Posts elements
You must connect behavior to your database model (Active Record)
use oks\tags\behaviors\TagsModelBehavior;
'tag_model'=> [
'class' => TagsModelBehavior::className(),
'attribute' => 'categoriesform',
'separator' => ',',
],
after
Вы должны настроить связи по примеру Постов
public function getTags()
{
return $this->hasMany(Tags::className(), ['tag_id' => 'tag_id'])->viaTable('poststags', ['post_id' => 'post_id']);
}
public function getPoststags()
{
return $this->hasMany(Poststags::className(), ['post_id' => 'post_id']);
}
потом вам нужно создать свойство для формы для обмена данных например
private $_tagsform;
...
public function getTagsform(){
return $this->_tagsform;
}
public function setTagsform($value){
return $this->_tagsform = $value;
}
если у вас уже создана можете использовать своё
View
use oks\tags\widgets\TagsWidget;
...
echo TagsWidget::widget([
'model_db' => $model,
'nameform' => 'Posts[tagsform]'
]);