Just Update Changed Fields In Table

I have a form which created manually without any CActiveRecord or CFormModel in behind it. I myself handle it in model layer.

NOW

How can i just update changed value fields ?

I found one way.

i put the current record in session and before updating that record compare new value returned by $_POST and the data stored in session.

Is it correct? possible? any better way?

At last i myself find a way. like so:

in view file i set current data in session.




$model = $model->getQuestions('q.id=:id', array(':id' => $question_id));

Yii::app()->session[$modelName] = $model[0];



And in controller i fetch new data from $_POST and detect changed field by array_diff. so easy:




if (isset($_POST[$modelName], Yii::app()->session[$modelName])) {

        	$fields = array_diff($_POST[$modelName], Yii::app()->session[$modelName]);

        	array_unshift($fields, $_POST[$modelName]['id']);

        	$fields['parent_id'] = $_POST[$modelName]['parent_id'];

        	if ($modelQuestion->validate($fields, FALSE))

            	if ($modelQuestion->save($fields, FALSE))

                	$this->redirect(array('generator/index', 'form' => $this->_form_id));

            	else

                	Yii::app()->user->setFlash('save_error', 'Saving failed, maybesome fields are not validate!');

    	}



It works fine.

Is there any better or optimize way?