updateByPk() - possible bug ?

Recently I’ve notice strange thing about UpdateByPk() method, maybe it’s mine error or a bug…

If I want to update I field in database like below:


$tmp=users::model()->updateByPk($model->id,array('avatar'=>$model->avatar));

it just doesn’t work for me and updateByPk() method returns 0.

If works fine, when I do it like that:




users::model()->updateByPk($model->id,array('avatar'=>NULL));

$tmp=users::model()->updateByPk($model->id,array('avatar'=>$model->avatar));



What I’m missing ?

It should be noted that this method returns the number of rows affected by the query, not whether the statement ran successfully or not.

If the value you are sending for the update is the same as the value currently in the database, it will return 0. If you are changing the value it should return the number of rows that are updated.

I think why it works in the second instance and possibly not in the first is because you are changing the value to NULL first so that it updates the value on the second query. I’m not sure about the first.

I am receiving absolutely the same "bug", I would say.




$count = Project::model()->updateByPk(

	$model->id,

	array('name'=>$model->name),

	'user_id = :user_id',

	array(':user_id'=>$model->user_id)

);


if ($count == 1)

{

	$results['status'] = self::SAVE_SUCCESS;

}

else{

	$results['count'] = $count;

}



And guess what. updateByPk return 0 while the row in the table is updated to the new value. I tested this dozens of times and got all the same 0.

I use Yii 1.1.6 on Windows with MySQL 5.5.