搜索
熱搜: 活動 交友 discuz
查看: 7878|回復: 0
打印 上一主題 下一主題

[PHP] [Yii2] 資料庫三層使用範例 - Database Uasge Demo of Yii

[複製鏈接]
跳轉到指定樓層
1#
發表於 2015-6-7 19:07:20 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Yii2 work with Database

  1. /**
  2. * ================================================================================
  3. * Database Uasge Demo of Yii
  4. * ================================================================================
  5. */
  6. public function actionDb()
  7. {
  8.    
  9.     /**
  10.      * ================================================================================
  11.      * Command Level
  12.      * ================================================================================
  13.      */
  14.     # Get db componet of Application
  15.     $db = Yii::$app->db;
  16.     // var_dump(\Yii::$app->id);exit;

  17.     $posts = $db->createCommand('SELECT * FROM video_source_company')
  18.         // ->rawSql;    // Get rawSql property
  19.         ->queryAll();

  20.     // print_r($posts);


  21.     /**
  22.      * ================================================================================
  23.      * SQL Builer Level
  24.      * ================================================================================
  25.      */
  26.     $rows = (new \yii\db\Query())
  27.         ->select(['*'])
  28.         ->from('video_source_company')
  29.         ->where([])
  30.         ->limit(10)
  31.         // ->createCommand()->rawSql;   // Call Command before all() does and get rawSql property
  32.         ->all();

  33.     // print_r($rows);


  34.     /**
  35.      * ================================================================================
  36.      * Model with Active Record Level
  37.      * ================================================================================
  38.      */
  39.     $activeRecords = \app\models\cms\Cms::find()    // Using Model
  40.     // ->createCommand()->rawSql;
  41.     ->createCommand()->queryAll();  // Using Comman level without ORM
  42.     // ->all();

  43.     print_r($activeRecords);
  44. }
複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-20 00:10 , Processed in 0.015384 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表