Logic Delete

Hi, I’m trying to implement a logic delete:

A field named active gets 0 when record is deleted, otherwise the default value is 1.

Question is, can I set a param in filters so when you look for a model it only takes the ones with active = 1?

Thanks

In your model:


public function rules()

{

   return array(

       //Other rules

       array('active', 'boolean', 'strict'=>TRUE, 'message'=>'Only boolean !'),

   );

}


//other stuff


public function search()

{

    $criteria=new CDbCriteria;

    //Other criteria

    $criteria->compare('t.active', $this->active);

    return new CActiveDataProvider($this, array(

        'criteria'=>$criteria,

        'pagination'=>array('pageSize'=>10),

    ));

}

And in your "admin" view:


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'users-grid',

    'dataProvider' => $model->search(),

    'enablePagination' => true,

    'filter' => $model,

    'columns' => array(

        //Other columns

        array(

            'name'=>'active',

            'filter'=>array(0=>'No', 1=>'Yes'),

            'type'=>'boolean',

            'htmlOptions'=>array('style'=>'text-align:center'),

        ),

        'class'=>'CButtonColumn',

    ),

));

And finally in your ‘_search’ view:


<div class="row">

<?php echo $form->label($model,'active'); ?>

<?php echo $form->checkBox($model,'active', array('checked'=>'checked', 'uncheckValue'=>0)); ?>

</div>

Now, you will have an ‘Active’ column with ‘No/Yes’ filter.

And in the ‘components’ section of your config/main.php file you may add:


'format' => array(

    'booleanFormat' => array('<span style="color:red">No</span>','<span style="color:green">Yes</span>'),

),

Thank’s that you will have all “No” in red color and all “Yes” in green color!