Cactiverecord Saveattributes() Returns Adverse Result

saveAttributes() method in CActiveRecord returns false when save occurs successfully.

WHY?

Impossible. Plz show your code.

Thanks for your consideration.

It’s a simple code .




				$vote = $article->Vote + $newVote;

				

				if(!$article->saveAttributes(array('Vote'=>$vote))){//this saves votes successfully but if I remove '!' , it doesn't

					//if vote saved successfully make the result to return

					$result = array();

					$result['contentUrl'] = $url;

					$result['contentTitle'] = $title;

				}

				else

				        die(print_r($article->errors)); 

					



I also tried this:




if($article->saveAttributes(array('Vote'=>$vote)) == false)



It saves successfully and pass the condition.

This is second time I meet this problem and I can’t figure it out.

Weird.

Do you have last version of Yii?

Also make sure you have not overriden updateByPk somewhere.

I’m using last version of Yii and no other update process somewhere else. Everything is simple and clear like crystal. I’m reading saveAttributes() method’s body but I can’t understand what is wrong.

Basically, this means that updated rows count is zero.

What DB do you have?

MySQL and InnoDB table type.

This doesn’t make sense, I’m giving up.

You need to set up some hooks to debug the core code and see how and when false appears.

Start with updateByPk inside saveAttributes and see how many rows are actually updated.

Maybe something’s wrong with your mysql installation or pdo.

Haven’t seen that before, though.

I replaced it with save() in my project.

I will do a full test and write the result here asap. Thank you.

I came across something similar today, I think it’s a MYSQL issue.

saveAttributes() returns the MYSQL rowcount of the rows affected ie changed, if the updated value is the same as the one in the database, then no rows have actually changed, and the rowcount will be 0.

saveAttributes() basically returns true or false depending on whether PDO->rowCount > 0

There is a stack overflow question 6237212 (I cant post links) about it.