The query builder that comes with Yii is very easy to use. It saves you the process of spelling sql. I encountered such a problem when writing a statement today
<span $connection</span> = Yii::app()-><span db; </span><span $command</span> = <span $connection</span>-><span createCommand(); </span><span $operate_rst</span> = 0<span ; </span><span if</span>(!<span empty</span>(<span $_POST</span>['lid'<span ])){ </span><span $operate_rst</span> = <span $command</span>->update('emg_landing', <span $landing_info</span>, 'lid=:lid', <span array</span>(':lid' => <span $_POST</span>['lid'<span ])); } </span><span else</span><span { </span><span $operate_rst</span> = <span $command</span>->insert('emg_landing', <span $landing_info</span><span ); } </span><span $connection</span>->active = <span false</span><span ; </span><span if</span>(<span $operate_rst</span> > 0<span ){ Functions</span>::returnOk('OK!'<span ); }<br />Functions::returnErrorJson();<br /></span>
Use $operate_rst to record the operation results. There is no problem when executing a new insert. However, when updating, sometimes it will show that the operation failed. After checking for a long time, I can't find the reason, so I have to go to the document
http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail
When you see the return item, it is
{<span return</span>} <span integer</span> <span <strong>number of rows affected by the execution.</strong></span>
I immediately understood the problem, because sometimes the data may not be changed but the update operation is triggered, so the number of rows changed at this time is 0, and the returned judgment will enter the error code. .
Similarly, the meaning of the return value of delete() and insert() methods is also the number of affected rows, so delete and insert can judge whether the operation is successful based on whether the return value is greater than 0, but the update operation is not necessarily the return value. A value of 0 may also indicate that the DB operation was successful.