sjaakp/yii2-wordcount 问题修复 & 功能扩展

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

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

sjaakp/yii2-wordcount

最新稳定版本:1.0.1

Composer 安装命令:

composer require sjaakp/yii2-wordcount

包简介

Word count behavior for Yii2.

README 文档

README

Word count behavior for Yii2

Latest Stable Version Total Downloads License

This is a word counting behavior for ActiveRecords in the Yii 2.0 PHP Framework. It counts the words in one or more designated attributes. The count(s) are exposed through new virtual attributes.

A demonstration of yii2-wordcount is here.

Installation

The preferred way to install yii2-wordcount is through Composer. Either add the following to the require section of your composer.json file:

"sjaakp/yii2-wordcount": "*"

Or run:

composer require sjaakp/yii2-wordcount "*"

You can manually install yii2-wordcount by downloading the source in ZIP-format.

Using WordCount

WordCount is a Behavior for an ActiveRecord. It has one property:

  • $attribute string|array The name of the attribute of wihich we want to count the words. Can also be an array of attribute names. Moreover, it can be an array with '<attrName>' => '<countAttrName>' elements.

If the count attribute name is not explicitly set in the $attribute array, the virtual count attribute is called '<attrName>_count' automatically.

Here is the simplest way to set up an ActiveRecord with WordCount:

namespace app\models;

use yii\db\ActiveRecord;
use sjaakp\wordcount\WordCount;

class Article extends ActiveRecord
{
    public static function tableName()
    {
        return 'article';
    }
    // ...
        
    public function behaviors()
    {
        return [
            [
                'class' => WordCount::class,
                'attribute' => 'bodytext'
            ],
            // ... other behaviors ...
        ];
    }
    // ...
}

Class Article will now have a new virtual attribute with the name 'bodytext_count'. It's value is an integer and it can be queried just like any other attribute:

$wordsCounted = $model->bodytext_count

A slightly more involved way to set up an Activerecord with WordCount would be:

 // ...     
 class Article extends ActiveRecord
 {
     // ...
         
     public function behaviors()
     {
         return [
             [
                 'class' => WordCount::class,
                 'attribute' => [
                    'bodytext' => 'textcount',
                    'title' => 'titlecount'
                 ]
             ],
             // ... other behaviors ...
         ];
     }
     // ...
 }

It gives two new virtual attributes, named 'textcount' and 'titlecount'.

Notice that WordCount uses the PHP function str_word_count(). This is not the most perfect way to count words, so you should consider the results as no more than good approximations.

Totals

Totals is a helper class with one method:

public static function count($query, $attribute)

This static function returns the total of $attribute values in the ActiveRecords found by ActiveQuery $query. If $attribute is a string, the return value will be an integer. If $attribute is an array of attribute names, count() will return an array with '<attr>' => <total> elements.

Usage example:

use sjaakp\wordcount\Totals;

$totals = Totals::count(Article::find(), [ 'titlecount', 'textcount' ]);

Notice that count() also works with non-virtual attributes. However, it would be much wiser to use ActiveQuery::sum() in that case.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固