Just curious to see if there is an alternative way for detecting to see if the user has changed the values in a model before it saves.
Currently right before the $model->save() happens, I have a block of code that goes out to the database and grabs the same record to do a before and after comparison.
$criteria = new CDbCriteria;
$criteria->condition='id=:id';
$criteria->params=array(':id'=>$model->id);
$oldModel = entry::model()->find($criteria);
if($model->save())
{
$model->determineState($oldModel); // where we determine if anything changed
$this->redirect(array('view','id'=>$model->id));
}
Onman
(O Rijkers)
February 1, 2010, 10:17am
2
Assuming the model is derived from CActiveRecord:
CActiveRecord has a property tableSchema which is of type CDbTableSchema.
In CDbTableSchema is a property columnNames.
Add a private property $_dataChanged to your model which is set to true (in __set()) when a value changes.
Override the magic method __set():
public funcion __set($prop)
{
if(in_array($prop,$this->tableSchema->columnNames)){
$this->_dataChanged=true;
}
parent::__set($prop);
}
To reset the $_dataChanged property:
public function afterSave()
{
$this->_dataDhanged=false;
}
Onman:
Assuming the model is derived from CActiveRecord:
CActiveRecord has a property tableSchema which is of type CDbTableSchema.
In CDbTableSchema is a property columnNames.
Add a private property $_dataChanged to your model which is set to true (in __set()) when a value changes.
Override the magic method __set():
public funcion __set($prop)
{
if(in_array($prop,$this->tableSchema->columnNames)){
$this->_dataChanged=true;
}
parent::__set($prop);
}
To reset the $_dataChanged property:
public function afterSave()
{
$this->_dataDhanged=false;
}
I need to look for specific variables that have changed, but I think this will work perfectly. Thanks for your help.
jerry2801
(Jerry2801)
February 2, 2010, 1:24am
4
you can record the old values on afterFind(),
and add determineState() function in your model, compare and get the result~
jerry2801:
you can record the old values on afterFind(),
and add determineState() function in your model, compare and get the result~
I could also do this, thanks.
yiipeter
(Peter)
December 27, 2013, 7:21am
6
Onman:
Override the magic method __set():
public funcion __set($prop)
{
if(in_array($prop,$this->tableSchema->columnNames)){
$this->_dataChanged=true;
}
parent::__set($prop);
}
Thanks for your answer! It works for me too.
But take a look to the magic method __set specification:
Without second parameter $value I get the PHP error.
public void __set ( string $name , mixed $value )