Not An Optimal Record Update Through Activerecord

        $objTovar = Tovar::model()->findByPk(62747);

        $objTovar->setAttribute('name', 'new_name');


Sql query

system.db.CDbCommand.execute(UPDATE `tovar` SET `id`=:yp0, `categories_id`=:yp1, `name`=:yp2, `shtrih`=:yp3, `brend_id`=:yp4, `suma`=:yp5, `massa`=:yp6, `unit_measure_id`=:yp7, `user_id`=:yp8, `is_admin`=:yp9, `is_active`=:yp10, `rating`=:yp11 WHERE `tovar`.`id`='62747')

Why did he updates the other fields? if it needs to update only ‘name’ ?

Dear Friend

To update only selected attributes in a table,we have to call AR::saveAttributes method.

$objTovar = Tovar::model()->findByPk(62747);


But this will not ensure validation.

One another way with method save.

$objTovar = Tovar::model()->findByPk(62747);


$objTovar->save(true,array("name"));//ensures validation.

$objTovar = Tovar::model()->findByPk(62747);


$objTovar->save(false,array("name"));//without validation.


It’s a known behaviour of Yii 1.x. Yii doesn’t keep track on which attributes got changed and simply updates all the fields. Yii 2 will be different in this regard.

Yii 2 - when it comes out?

When it’s done™. There is no release date yet ;)

use saveattributes(array()) ;