Yii Framework Forum: Logic Delete - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Logic Delete Rate Topic: -----

#1 User is offline   taz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 06-January 13

Posted 18 January 2013 - 04:25 PM

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
0

#2 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 14-August 12

Posted 18 January 2013 - 05:32 PM

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.
0

#3 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 14-August 12

Posted 18 January 2013 - 05:36 PM

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!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users