fduch2k/yii-flagged-activerecord

扩展 CActiveRecord 类以添加位字段操作功能

0.2.2 2015-07-14 10:23 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:57:43 UTC


README

扩展 CActiveRecord 类以添加位字段操作功能。 变更日志

安装

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

运行以下命令之一

php composer.phar require --prefer-dist fduch2k/yii-flagged-activerecord "*"

或添加以下内容到您的 composer.json 文件的 require 部分。

"fduch2k/yii-flagged-activerecord": "*"

to the require section of your composer.json.

使用方法

class Article extends TSFlaggedActiveRecord 
{
    //...
    // By default flag field has name 'flags', you can override it 
    // if your prefer other name
    
    // public $flagsField = 'flags';

    // By default flags values without specified bit computed automatically 
    // (draft => 1, published => 2, deleted => 128)
    public function flags() 
    {
        return array(
            'draft',
            'published',
            'deleted' => 128
        );
    }
    
    // Flag labels uses in interface messages
    // By default an flag label is generated using
    // CModel::generateAttributeLabel
    public function flagLabels()
    {
        return array(
            'deleted'=>'Removed'
        );
    }

}

现在您可以在代码中使用它

作用域

// Find all published articles
$articles = Article::model()->published()->findAll();

// or all drafts
$articles = Article::model()->withFlag('draft')->findAll();

// or deleted drafts
$articles = Article::model()->withFlag('draft, deleted')->findAll();

// or not deleted
$articles = Article::model()->withoutFlag('deleted')->findAll();

标志获取/设置器

$article = Article::model()->findByPk(10);
// Check if article is not deleted...
if ($article->isDeleted === false) {
    //...then publish it
    $article->isPublished = true;
}
$article->save();

获取标志值

echo Article::model()->getFlag('deleted'); // outputs 128

将标志条件应用到条件中

// get criteria to find not deleted article drafts
$criteria = Article::model()->applyFlags(new CDbCriteria(), array('draft', '!deleted'));

变更日志

###0.2.2 / 2015-07-14

  • 修复生成标志文本表示的问题

###0.2.1 / 2014-12-04

  • 添加获取标志名称的功能

###0.2.0 / 2014-11-21

  • 覆盖 getAttributes 和 setAttributes 方法以实现标志功能
  • 添加 getFlagNames 方法
  • 方法 setFlag 现在可以正确处理布尔字符串 'true' 'false'(等于 'true' 的字符串为真,否则为假)
  • 添加忽略的 osx 特定文件