Yii 1.1: statesbinaryfieldbehavior

CActiveRecordBehavior for converting checkBoxList data to INT database field
3 followers

Converts data of multiple checkboxes to INT representation by setting bits of integer value (like at *nix chmod command).

Usage

ActiveRecordModel:

public function behaviors()
    {
        return array(
            'StatesBinaryField'=>array(
                'class'=>'path.to.StatesBinaryFieldBehavior',
                'attribute'=>'states', // Model attribute name
                'data'=>array(         // Source data of available states
                    'state1'=>'First model state',
                    'state2'=>'Second model state',
                    'state3'=>'Third model state',
                ),
            )
        );
    }

View file:

$model=ARModel::model()->findByPK(1); // in controller of course
 
echo CHtml::beginForm();
echo CHtml::activeCheckBoxList($model, 'states', $model->statesData);
echo CHtml::submitButton();
echo CHtml::endForm();

Checking state (in any model attribute format [int or arr]):

if ($model->checkState(array('state1'))) echo 'Model state 1 is set';
if ($model->checkState(array('state1','state3'))) echo 'Model state 1 and 3 is set';

Force attribute convertion:

$model->convertAttribute() // or
$model->convertAttribute('auto') // converts to opposite format
$model->convertAttribute('int') // coverts to database int representation
$model->convertAttribute('arr') // converts to output array representation

Selecting:

Model::model()->statesIncluded(array('state1','state3'))->findAll();
/*
    Will select records:
        ('state1','state2','state3')
        ('state1','state3')
    Not:
        ('state1','state2')
*/
 
Model::model()->statesExact(array('state1','state3'))->findAll();
/*
    Will select records:
        ('state1','state3')
    Not:
        ('state1','state2','state3')
        ('state1')
*/
 
Model::model()->statesAtLeast(array('state1','state3'))->findAll();
/*
    Will select records:
        ('state1','state2','state3')
        ('state1')
        ('state3')
    Not:
        ('state2')
*/

Using out of ARModel for forming sql WHERE statement:

$states=array('state1','state3');
$data=array(
    'state1'=>'First model state',
    'state2'=>'Second model state',
    'state3'=>'Third model state',
);
$bitmask=StatesBinaryFieldBehavior::createBitMask($states,$data);   // Returns integer: 5
$dbColumn='table.column'; // Database table column alias
$where_Included     ="$dbColumn & $bitmask = $bitmask";
$where_Exact        ="$dbColumn = $bitmask";
$where_AtLeast      ="$dbColumn & $bitmask != 0";

Other usage moments

  • Massive attributes assignment available.

  • Model saving at controller makes as ususal.

  • No need for attribute validation, just set it as save-attribute.

  • Normaly attribute contains an array of selected(setted) states, like array('state1','state3') and converts it to int representation only before saving.

  • Order of state keys at requested $states doesn't matter. Example: Model::model()->statesAtLeast(array('state1','state3')) equal to Model::model()->statesAtLeast(array('state3','state1'))

  • Only one field per model available (yet).

  • You a free to rename keys and labels of source $data without affecting to database records, BUT NOT THEIR ORDER.

Be the first person to leave a comment

Please to leave your comment.

Create extension