Home  >  Article  >  Backend Development  >  Detailed explanation of the steps to use Yii framework builder, update and delete

Detailed explanation of the steps to use Yii framework builder, update and delete

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 09:34:411487browse

This time I will bring you the yii frameworkbuilder, update, deleteDetailed explanation of the steps to use, the precautions for using the yii framework builder, update, and deleteare Which ones, the following are practical cases, let’s take a look.

I encountered such a problem when writing a statement:

$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::
return
Ok('OK!');
}
Functions::returnErrorJson();

Use $operate_rst to record the operation results. There is no problem in executing a new insert, but when updating, sometimes it will be displayed The operation failed. After checking for a long time, I couldn't find the reason. I had to go to the document

http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail

and saw the return item:

{return}    
integer
    number of rows affected by the execution.

Sometimes the data may not have been changed but The update operation is triggered, so the number of changed rows at this time is 0, and the returned judgment enters the error code.
Similarly, the meaning of the return value of the delete() and insert() methods is also the number of affected rows, so delete and insert can determine whether the operation is successful based on whether the return value is greater than 0, but update operationNot necessarily. A return value of 0 may also indicate that the DB operation was successful.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

PHP7 uses lib library case code analysis based on function method

How to make an addable function in PHP Classes for watermarking and generating thumbnails

The above is the detailed content of Detailed explanation of the steps to use Yii framework builder, update and delete. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn