BUG in AR update

Hello! Sorry for my English!

In manual im see -




Post::model()->update($attributes,$condition,$params);



In the above, $attributes is an array of column values indexed by column names;

I think this array should look like


 $arr['field_name'] => 'new_field_value'

… yes?

I look source code … and what i see?




public function update($attributes=null)

	{

		if($this->getIsNewRecord())

			throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));

		if($this->beforeSave())

		{

			Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord');

			if($this->_pk===null)

				$this->_pk=$this->getPrimaryKey();

				


			$this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));

			$this->_pk=$this->getPrimaryKey();

			$this->afterSave();

			return true;

		}

		else

			return false;

	}



The main thing we need is there’s this line




$this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));



pass to another method. Our array is processed using $ this-> getAttributes ():

Is a part source code:




if(is_array($names))

		{

			$attrs=array();

			foreach($names as $name)

			{

				if(property_exists($this,$name))

					$attrs[$name]=$this->$name;

				else

					$attrs[$name]=isset($attributes[$name])?$attributes[$name]:null;

			}

			return $attrs;

		}



The main thing we need is




$attrs[$name]=$this->$name;



The new array is formed on the basis of values​​!

that is -

It was: $arr[‘field_name’] => ‘new_field_value’

It has become: $arr[‘new_field_value’] => NULL

Please explane me about this!

Hi,

You are mixing up “update()” and “updateAll()”. :)

http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#update-detail

Why, then, need update () ?

If possible, show an example

From update() doc:




  $attributes	array	list of attributes that need to be saved. 

  Defaults to null, meaning all attributes that are   loaded from DB will be saved.



Method is used when you need to update single model and you have instance of this model.

Usage:




  $post = Post::model()->findByPk(1);

  $post->read_count = 2;

  $post->update(array('read_count'));



Note that usually you need save() method to save single model (will atomatically decide what to do - insert or update).