oks/yii2-tags

标签

安装: 3

依赖: 0

建议者: 0

安全: 0

类型:yii2-extension

dev-master 2018-10-13 08:02 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:52:31 UTC


README

标签

安装

安装此扩展的首选方式是通过 composer.

运行

php composer.phar require --prefer-dist oks/yii2-tags "*"

或者

"oks/yii2-tags": "*"

添加到你的 composer.json 文件的 require 部分。

用法

扩展安装完成后,只需在你的代码中使用它即可

你需要连接 i18n 以进行翻译

 'oks-tags' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@vendor/oks/yii2-tags/src/messages',
                    'sourceLanguage' => 'en',
                    'fileMap' => [
                        'oks-tags'       => 'main.php',
                    ],
                ],

并迁移数据库

yii migrate --migrationPath=@vendor/oks/yii2-tags/src/migrations

更新(Active Record)- 单个

与 Posts 元素示例

您必须在之后将行为连接到您的数据库模型(Active Record)

   
  use oks\tags\behaviors\TagsModelBehavior;
  

 'tag_model'=> [
                        'class' => TagsModelBehavior::className(),
                        'attribute' => 'categoriesform',
                        'separator' => ',',
                        ],

之后

您必须按照示例设置关系,例如 Posts

    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;
            }

如果您已经创建了,则可以使用自己的

视图

use oks\tags\widgets\TagsWidget;


...


echo TagsWidget::widget([
        'model_db' => $model,
        'nameform' => 'Posts[tagsform]'
        ]);