Saving unchanged data

Hi everybody!

I’m new to yii, I’m still investigating how suitable the framework is for my purposes.

My question is simple. I load an AR from the DB to allow the user to modify it. Does somehow the AR object checks if the AR was changed before saving?

After checking the code it seems to me that there is no check for this. Practically I could not find any code that would represent the state of the AR with the exception of representing whether the AR is new or a loaded record.

Why don’t you give it a try? :)

For example using FireBug and WebLogRoute, you can see queries sent to the database.

I just did it:

[17:36:18.878][trace][application] START AR TEST

[17:36:18.915][trace][system.db.CDbCommand] Executing SQL: UPDATE News SET id=:yp0, title=:yp1, body=:yp2, author=:yp3, date=:yp4, teaser=:yp5, teaserImage=:yp6, isPublic=:yp7, visits=:yp8 WHERE

[17:36:18.915][trace][application] STOP AR TEST

It updates unchanged records in the database.

Thanks for the answer! I’m new to the LAMP world. I’ve spent 2 hours to find a MySql profiler without too much success. But you gave me the opportunity to get know a new class CWebLogRoute :)

Anyway, adding such a feature - managing AR state - seems to be easy and could improve db performance as well.

I do not think it would make much difference… Actually I think it would be a "bloat" feature.

e.g, why would a user even click "update" if he/she did not change any fields on a said update form? I know I am only looking at one case, but I can not think of any cases where this feature would help.

Sometimes programmers want models saved even if no data was changed. For instance, maybe they got something going on in beforeSave() that changes something

when change data in user model, the field password is a problem

another topic:

Why when I leave a field to null in the view, in the database appears as empty string?

This has already been discussed, so try some forum recherche. There where some solutions for this. I even thought we had an extension for it but can’t find it now. A basic implementation as a behaviour would be pretty easy. As Yii tries to stay as slim as possible you’ll probably never see this in the core.


This is a “well know problem” ;) and it also has some well known solutions. Spend some minutes on a forum search and you’ll find.

I created a behavior to track changes so I could auto-save.

It’s here:

It’s part of my extension library.

Check you rlink… I’m getting this error:

The requested URL /p/ps-yii-extensions/source/browse/trunk/extensions/pogostick/behaviors/CPSDeltaChangebehavior.php was not found on this server.


yes, it was just one example

make it clear: I love yii

@gergomeister:look this , surely this code help you