Yii with the Query Builder is still very useful, save the process of the spelling of SQL, today in writing a statement of the time encountered such a problem
Copy Code code as follows:
$connection = Yii::app ()->db;
$command = $connection->createcommand ();
$operate _rst = 0;
if (!empty ($_post[' lid ')) {
$operate _rst = $command->update (' emg_landing ', $landing _info, ' lid=:lid ', Array (': Lid ' =>, $_post[' lid '));
}
else{
$operate _rst = $command->insert (' emg_landing ', $landing _info);
}
$connection->active = false;
if ($operate _rst > 0) {
Functions::returnok (' ok! ');
}
Functions::returnerrorjson ();
Use $operate _rst to record the results of the operation, the implementation of the new insert no problem, but at the time of the update, sometimes show the operation failed, check for a half-day, also can not find the reason, had to go to the document
Http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail
See return that one is
Copy Code code as follows:
{return} An integer number of rows affected by the execution.
Instant understanding of the problem, because sometimes there may not be changes to the data but triggered the update operation, so this time the changed number of rows is 0, the return of the judgment is entered into the error code.
Similarly, the Delete () and insert () methods return value meaning is also the number of rows affected, so delete and insert can determine whether the operation succeeded based on whether the return value is greater than 0来, but the update operation does not necessarily mean that the return value of 0 may also indicate success for the DB operation.