update record only when something changed?

Hi

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?

somebody please?

Hello. You may want to see this thread:

http://www.yiiframework.com/forum/index.php/topic/18480-determine-which-attributes-are-changed-of-an-activerecord/

and this wiki:

http://www.yiiframework.com/wiki/9/how-to-log-changes-of-activerecords/

Good luck

hi!

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 :rolleyes: 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?

thank you very much!

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


private $_oldAttributes = array();

do you think I can use this idea?

do you mean this (How to log changes of ActiveRecords?)?

okay, I’m going to try this. I’m going to keep you updated!

So you say, you think it’s better to do this comparison in the model, instead of in the controller, right? why? or what is the difference?

I don’t know why, I can’t explain it, but for me it seems more suitable to put this comparison into the controller… :unsure:

Thanks a lot!

UPDATE: I hate when I can’t get to the next step. I can’t find the solution for now. :angry:

there should be some easy built-in method in yii…

Well I think that is for comparing two records, not the modifications on one record. But I may be wrong.

yes!

Don’t worry, do what you feel comfortable with.

Don’t be desperate :slight_smile: What have you done till now with all of the above?

well I have read it but I didn’t have a “Yes It Is!” feeling… :rolleyes:

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?