Yii 1.1: listbehavior

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

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!

Total 4 comments

#18281 report it
dragan.zivkovic at 2014/10/10 04:28pm
Good job

Thanks for extension

#12490 report it
Nur Rochim at 2013/03/23 12:21pm
AIMAGU

Wow.... best extension! thanks :)

#8014 report it
jmariani at 2012/05/03 02:15pm
Cool!

I like this extension. Thank you very much!

#7876 report it
JFReyes at 2012/04/24 09:02am
Works nicely

This will save me some work as I use a few of these data structures. Thanks!

Leave a comment

Please to leave your comment.

Create extension