listbehavior Easy working with status-like attributtes of active record (represent text and html for integer values)

  1. Requirements
  2. Usage
  3. Additional
  4. History

Let's assume you have active record with Status attribute. Status can be Created, Confirmed and Rejected. In database table you have column status_id and store ID of each status according to some map, e.g.
ID Status
0 Created
10 Confirmed
20 Rejected

Screenshot

There are two ways of storing text representation of statuses:

  1. create additional table in database, e.g. tbl_status (id, text), create Status model and setup relation
  2. store in php code as constants (and this extension should help)

If you have many statuses or they are changing frequently I suggest to use first way.
But if statuses more or less fixed and you don't want to create additional table/model you can try ListBehavior. With it you can easliy use constants (such as STATUS_CREATED etc.), show text and html representations and get array with all values (for dropdown).

Requirements

Developed and tested on Yii 1.1.10

Usage

1.Copy ListBehavior.php into protected/components folder.
2.Create your class extending ListBehavior like this:

class StatusBehavior extends ListBehavior 
{
    const CREATED                = 1;
    const CONFIRMED              = 10;
    const CANCELLED              = 20;

    public function data()
    { 
        return array(
            self::CREATED          => array('text' => 'Created',     'color' => 'gray'),
            self::CONFIRMED        => array('text' => 'Confirmed',   'color' => 'green'),  
            self::CANCELLED        => array('text' => 'Rejected',    'color' => 'red'),  
        );     
    }

   /* or if you need only text
    public function data()
    { 
        return array(
            self::CREATED          => 'Created',
            self::CONFIRMED        => 'Confirmed',
            self::CANCELLED        => 'Rejected',  
        );     
    }
   */
}

You can put this file to models directory as it's looks like tiny model.

3.Attach your behavior to active record (attribute parameter is requred):

class Document extends CActiveRecord
{
...
    public function behaviors()
    {
        return array(      
           'status' => array(
                'class' => 'StatusBehavior',
                'attribute' => 'status_id',
            ),
        );
    }     
}

4.That's it! Now you can use it via $model->status->text and $model->status->html.

$model = Document::model()->findByPk($pk);
echo $model->status->html;

Or in columns property of CGridView:

$this->widget('zii.widgets.grid.CGridView', array(
  'id'=>'grid-1',
  'dataProvider'=>$model->search(),
  'columns' = array(
     'created_date',
     'document_number',
     array(   
       'type' => 'raw',
       'header' => 'Document Status',
       'value' => '$data->status->html',
    ),
),
));

To get array with all possible values (e.g. for dropdown in form):

$listData = Document::model()->status->getList();
echo $form->activeDropDownList($model, 'status_id', $listData);

Additional

  • you can change html representation via parameter htmlTemplate. It's default value is:
<span style="color: {color}">{text}</span>

There can be any tokens taken from array keys of data() method.

  • you can change text for not-found values via undefinedText param.
  • you can easily use constants like this way:
if($model->status_id == StatusBehavior::CREATED) {
...
}

History

May, 8
Redesigned and fixed #8032

April, 23
Initial release

All comments are welcome!

9 2
14 followers
699 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: Vitalets
Created on: Apr 23, 2012
Last updated: 6 years ago

Downloads

show all

Related Extensions