kit-tools/yii2-tags 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kit-tools/yii2-tags

Composer 安装命令:

composer require kit-tools/yii2-tags

包简介

Tags extensions

关键字:

README 文档

README

The extension allows you to add tags to different models. Counts the weight of tags and the number of clicks per tag for each model. There is also an admin panel for adding tags. And a few widgets to display tags.

Installation

The preferred way to install this extension is through composer.

Either run:

composer require kit-tools/yii2-tags

or add

"kit-tools/yii2-tags": "^1.0.0"

to the require section of your composer.json file.

Usage

Admin

Add to config (config/main.php или config/web.php)

'controllerMap' => [
    'tag' => 'kittools\tags\controllers\TagController'
],

Property $tagList

In the model with which you want to associate tags, add:

<?php
...
/**
 * @var array list of tags entered by the user.
 */
public $tagList = [];
...

Validation rules

<?php
public function rules(){
    ...
    ['tagList', 'required', 'skipOnEmpty' => false],
    ['tagList', 'each', 'rule' => ['string'], 'message' => 'Tags are filled incorrectly.'],
    ...
}

If the tags are optional, then you need to delete the line

    ['tagList', 'required'],

Behavior

public function behaviors()
{
    return [
        ...
        'tagBehavior' => [
            'class' => TagBehavior::className(),
        ],
        ...
    ];
}

TagBehavior works with events:

  • ActiveRecord :: EVENT_AFTER_INSERT, ActiveRecord :: EVENT_AFTER_UPDATE - adds new tags, establishes / removes model and tag relationships.
  • ActiveRecord :: EVENT_AFTER_DELETE - when deleting a model, removes links with tags. Linked tags are not removed.

Important!!! If there are connected tags in the model, and the tags will not be transferred when saving, then all links with the tags will be deleted! To avoid this situation, before saving the model, you need to untie the TagBehavior

$model->detachBehavior('tagBehavior');

One-to-many relationship

<?php
use kittools\tags\models\Tag;
use kittools\tags\models\TagEntityRelation;
...
public function getTags()
{
    return $this->hasMany(Tag::class, ['id' => 'tag_id'])
        ->viaTable(TagEntityRelation::tableName(), ['entity_id' => 'id'], function($query){
            $query->andWhere([TagEntityRelation::tableName() . '.entity' => static::class]);
        });
}

The method adds a model link to odic-to-many tags through an intermediate table.

Form adding tags in creating / editing models

Add tag widget

<?php
use kittools\tags\widgets\TagInputWidget;
...
?>

...
<?= $form->field($model, 'tagList')
    ->widget(TagInputWidget::className(), [
        'pluginOptions' => [
            // maximum number of tags that can be selected
            'maximumSelectionLength'=> 5,
        ]
    ])
    ->hint('Press "Space", "," or Enter to separate tags from each other.')
    ->label('Tags'); ?>
...

The widget loads the tags with an ajax request. Minimum word length after which tags will be loaded, 2 characters.

or

<?php
use kittools\tags\widgets\TagInputWidget;
use yii\helpers\ArrayHelper;
use kittools\tags\models\Tag;
...
?>
...
<?= $form->field($model, 'tagList')
    ->widget(TagInputWidget::className(), [
        'pluginOptions' => [
            // maximum number of tags that can be selected
            'maximumSelectionLength'=> 2,
            // Minimum tag length for listing tags, default 2
            'minimumInputLength' => 0,
            // disabling tag loading by ajax request
            'ajax' => null,
        ],
        // Tags list
        'data' => ArrayHelper::map(Tag::find()->all(), 'title', 'title')
    ])
    ->hint('Press "," or "Enter" to separate tags from each other')
    ->label('Tags'); ?>
...

The widget does not load tags through an ajax request. A list of tags is loaded when the page loads.

Tag widget inherited from kartik\select2\Select2.

More information about widget settings can be found at https://select2.org/

Tag output widget

When using the widget in the list of materials, do not forget about the "problem of N + 1 queries".

<?php
use kittools\tags\widgets\TagsEntityWidget;
...
echo TagsEntityWidget::widget([
    'model' => $model,
    'caseRegister' => 'upper',
    'tagUrl' => ['site/tag'],
    'delimiter' => ', ',
 ]);
...

Detailed description of widget settings in a file kittools\tags\widgets\TagsEntityWidget

Tag cloud widget

<?php
use kittools\tags\widgets\CloudTagsWidget;
use app\models\News;
...
echo CloudTagsWidget::widget([
    'entity' => News::className(),
    'type' => CloudTagsWidget::TYPE_WEIGHT,
    'shuffleTags' => false,
    'tagUrl' => ['/news/tag'],
    'coefficientIncreaseFont' => 1,
    'tagPrefix' => '',
    'tagUrlOptions' => ['style' => 'color: #003366;', 'target' => '_blank']
]);
...

more information about widget settings can be found in kittools\tags\widgets\CloudTagsWidget

kit-tools/yii2-tags 适用场景与选型建议

kit-tools/yii2-tags 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tags」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kit-tools/yii2-tags 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kit-tools/yii2-tags 我们能提供哪些服务?
定制开发 / 二次开发

基于 kit-tools/yii2-tags 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-08-26