Cactiverecord Update() Returns True

Don’t know if any one has noticed, but Yii CActiveRecord class has a bug in the update() method within these lines of code.




return true;

So, the result of updateByPk() just goes unprocessed, and the method returns true whether updating has been successful or not.

As an independent method, CActiveRecord::updateByPk() may return 0 or 1, depending on the 3rd and 4th parameters ($condition and $params).

But when it is used in CActiveRecord::update(), those parameters are set to the default values (an empty string and an empty array). That means it can only return 1. So there’s no point in checking the return value.

In both of the cases, the updating may fail because of an exception.

CActiveRecord::updateByPk() uses CDbCommand::execute(). And as you see in the source code of CDbCommand::execute(), it may throw an exception.

But once an exception has been thrown, we will have no chance to check the return value of updateByPk() in update() method. Instead of that, we will catch the exception somewhere in our app code and do something, if we need.

This makes sense. Mine failed to update only because I instantiated a new record based on a number of form attributes, and later used these attributes to update a row in the db. Since I’m not calling find() to generate that record, its oldPrimaryKey is not given, and, therefore, failed to update. I guess we should check for oldPrimaryKey before calling updateByPk() in update().