yiiext/status-behavior

此包的最新版本(0.7)没有可用的许可证信息。

Yii框架Active Record状态行为,用于模型

0.7 2013-10-30 08:00 UTC

This package is auto-updated.

Last update: 2024-09-13 10:40:25 UTC


README

可用于与模型一起使用,添加管理模型状态的功能。

安装和配置

向模型和数据库表添加字段。以字段status为例。字段类型取决于行为配置。

配置模型

<?php
class Post extends CActiveRecord {
    public function behaviors() {
        return array(
            'statuses' => array(
                'class' => 'ext.yiiext.behaviors.model.status.EStatusBehavior',
                // Field used for status
                'statusField' => 'status',
                // Allowed statuses. Default is array('draft', 'published', 'archived').
                // One can pass an array where key is DB field name, value is what user will see.
                // 'statuses' => array('draft', 'published', 'archived'),
                // 'statuses' => array('d' => 'draft', 'p' => 'published', 'a' => 'archived'),
            ),
        );
    }
}

示例

配置模型

<?php
class Post extends CActiveRecord {
    public function behaviors() {
        return array(
            'statuses' => array(
                'class' => 'ext.CStatusBehavior.CStatusBehavior',
                'statusField' => 'status',
            ),
        );
    }
}

class Book extends CActiveRecord {
    public function behaviors() {
        return array(
            'statuses' => array(
                'class' => 'ext.CStatusBehavior.CStatusBehavior',
                'statusField' => 'status',
                'statuses' => array(
                  'new' => 'new',
                  'reserved' => 'reserved',
                  'sale' => 'sale',
                ),
            ),
        );
    }
}

使用状态

<?php
$post=Post::model()->findByPk(1);
// Getting current status
echo $post->getStatus();
// Changing status
$post->setStatus('draft');
// Saving model
if ($post->save() === FALSE) {
    echo 'Error!';
}

$post = Post::model()->findByPk(1);
// Changing status field only
$post->setStatus('draft')->saveStatus();