Fork me on GitHub

Status Behavior

Поведение для моделей, которым нужен статус, например статус постов.

Установка и настройка

Добавить поле в таблицу и модель. В примере, это поле status. Тип поля зависит от настроек поведения.

Сконфигурировать модель:

class Post extends CActiveRecord {
    public function behaviors() {
        return array(
            'statuses' => array(
                'class' => 'ext.yiiext.behaviors.model.status.EStatusBehavior',
                // Поле зарезервированное для статуса
                'statusField' => 'status',
                // Разрешенные статусы, по умолчнию array('draft', 'published', 'archived').
                // Передается массив, ключ каждого элемента массива для сохранения в БД, значение выводится пользователю,
                // 'statuses' => array('draft', 'published', 'archived'),
                // 'statuses' => array('d' => 'draft', 'p' => 'published', 'a' => 'archived'),
            ),
        );
    }
}

Примеры

Подключение

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',
                ),
            ),
        );
    }
}

Использование

$post=Post::model()->findByPk(1);
// Выводим статус
echo $post->getStatus();
// Изменяем статус
$post->setStatus('draft');
// Сохраняем модель
if ($post->save() === FASLE) {
    echo 'ошибки сохранения';
}
 
$post = Post::model()->findByPk(1);
// Изменяем статус cохраняем только поле status
$post->setStatus('draft')->saveStatus();

Changelog

0.6 Warning: this version is not backwards compatible to 0.5.

[*] Change setStatus() to took status value and status text. [-] Remove translate statuses. [-] Remove message component. Used 'yiiext' group for messages.

0.5 [] Yii:t() is now used to translate status. [] All private methods and properties transform in protected. [+] Added magic method __toString() to get status. [*] New naming conventions. [+] readme_en.

0.4 [-] Remove getStatusName method. [+] Added param to prevent translating in getStatuses and getStatusText methods.

0.3 [+] Added getStatuses method. [+] Added getStatusName method for return original status without translated.

0.2 [*] Small code corrections.

0.1 [+] Initial public release.

Provides methods and named scopes to work with model statuses.

Documentation

Downloads (Tags)

Resources