I would like to know something: basically, how does yii work when using update view and update function? When clicking on save button, does it compare if there were any changes according to the original values, or does it update every case?
if yii is not comparing, how can I achieve that update only run, when there is something changed compared to the original values?
can it be a solution to somehow cache values of the current record, before we arrive to the form when clicking on update? if yes, I can imagine to compare the two arrays quite easily.
I was thinking about logging into the db also. by some parts of the applications it will be useful, however, I would still think of only putting information into the log table, when something has really changed. and so, I’ve arrived back to the same problem.
another idea was to make a separate field in the db table, that would contain all the values of all the other fields concatenated so I would have a complete unique key. but this would be very unefficient to query and compare I guess.
on this link I think the guy has a different problem, however, the others were misunderstanding him and were answering to a different question, a question like mine. so for me, the answers for the misunderstood question seems to be good, however I’m not feeling myself able yet to implement:
I’m understanding the direction, but little bit confused, for example, what is afterFind() and why is it here.
In fact, this seems very easy, but I can’t figure it out yet, but in fact, for me every necessary data seems to be there:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Mrsk2']))
{
if ($model==$_POST['Mrsk2']) {throw new CHttpException(400,'you haven't changed anything');}
$model->mrsktd = new CDbExpression('NOW()');
$model->save();
$model=new Mrsk2;
$model->attributes=$_POST['Mrsk2'];
if($model->save())
$this->redirect(array('admin'));
}
$this->render('update',array(
'model'=>$model,
));
}
$model is containg the original values and $_POST[‘Mrsk2’] is containing the “new” values. How do I compare the two… things? (they are not arrays)
UPDATE:
$model_new=new Mrsk2;
$model_new->attributes=$_POST['Mrsk2'];
if ($model->field1==$model_new->field1) {throw new CHttpException(400,'you haven't changed field1');}
this way it’s working, but it’s not very… elegant this way I would have to repeat this for all fields. would you be so kind to give me some directions which way I should begin to search to make this more elegant?
Well I believe you’re achieving the same result in your controller, but with more efforts than in the model.
You’d put afterFind in your model. It is run just after the model is loaded (on update), so if you add that line of code ($this->_oldAttributes = $this->attributes), you’re basically saving the attributes that have just been fetched from the db in the _oldAttributes object.
Likewise, I’d personally use beforeSave instead of using the controller. But I’m not an MVC expert.
That said, I also think you can loop through the _oldAttributes and attributes objects (foreach … as …), that would save you from comparing field by field.
Please keep us updated.
Edit: Have you read through the wiki? It’s pretty well detailed as well.
PS Don’t forget to declare _oldAttributes in your model
well I have read it but I didn’t have a “Yes It Is!” feeling…
just made myself more confused…
I’m still curious: basically, how does yii work when using update view and update function? When clicking on save button, does it compare if there were any changes according to the original values, or does it run an sql update every case? or is it no problem for mysql, because maybe I can imagine mysql itself recognizes that he has nothing to do. Do we get a response from mysql that something happened? like when you get insert_id when inserting? can somebody explain it to me?