yii架構builder、update、delete用法樣本

來源:互聯網
上載者:User

Yii內建的query builder很好用,省去了拼sql的過程。

在寫一個語句的時候遇到這樣一個問題:

  1. $connection = Yii::app()->db;
  2. $command = $connection->createCommand();
  3. $operate_rst = 0;
  4. if(!empty($_POST['lid'])){
  5. $operate_rst = $command->update('emg_landing', $landing_info, 'lid=:lid', array(':lid' => $_POST['lid']));
  6. }
  7. else{
  8. $operate_rst = $command->insert('emg_landing', $landing_info);
  9. }
  10. $connection->active = false;
  11. if($operate_rst > 0){
  12. Functions::returnOk('OK!');
  13. }
  14. Functions::returnErrorJson();
複製代碼

用 $operate_rst 來記錄操作結果,執行建立insert沒有問題,但是在更新時候,有時會顯示操作失敗,檢查了半天,也找不到原因,只好去翻文檔http://www.yiiframework.com/doc/api/1.1/CDbCommand#update-detail看到return那一項:{return} integer number of rows affected by the execution.有時可能沒有改資料但是觸發了更新操作,所以這時候受更改的行數為0,返回的判斷就進入到錯誤碼裡。同理,delete() 和 insert() 的方法傳回值意義也是受到影響的行數,所以delete和insert可以根據傳回值是否大於0來判斷操作是否成功,但是update操作不一定,傳回值為0也有可能表示對DB操作成功。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.