Thursday, 18 December 2014

Handling NULL values in YII

In Yii,

The control get focus then the field values assigned as '' which is not considered as NULL in yii,

To make the field as exact null then, write the following code in controller as

public function actionCreate()
    {
           ..........
           ..........
        if(isset($_POST['Ddrc']))
        {
           ..........
           ..........
            if($model->action_taken=='')
                  $model->action_taken = null;
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
         }
           ..........
           ..........
}

Before saving the field if the field value matches with '' then assign null to the field so the query 

select * from table where action_taken is null gives the exact result 

Use the same lines in Update action also if the control presented in view.

public function actionUpdate($id)
{

           ..........
           ..........
           if($model->action_taken=='')
                  $model->action_taken = null;
           ..........
           ..........
}

--

No comments:

Post a Comment