nekulin
(Boblo1)
January 9, 2013, 6:48am
1
$objTovar = Tovar::model()->findByPk(62747);
$objTovar->setAttribute('name', 'new_name');
$objTovar->save();
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’ ?
seenivasan
(Chellamnivas)
January 9, 2013, 7:11am
2
Dear Friend
To update only selected attributes in a table,we have to call AR::saveAttributes method.
$objTovar = Tovar::model()->findByPk(62747);
$objTovar->saveAttributes(array("name"=>"new_name"));
But this will not ensure validation.
One another way with method save.
$objTovar = Tovar::model()->findByPk(62747);
$objTovar->name="new_name";
$objTovar->save(true,array("name"));//ensures validation.
$objTovar = Tovar::model()->findByPk(62747);
$objTovar->name="new_name";
$objTovar->save(false,array("name"));//without validation.
Regards.
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.
nekulin
(Boblo1)
January 9, 2013, 7:38am
4
Yii 2 - when it comes out?
When it’s done™. There is no release date yet
use saveattributes(array()) ;