最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Yii2數(shù)據(jù)庫(kù)操作常用方法小結(jié)

 更新時(shí)間:2017年05月04日 11:00:28   作者:yhdsir  
這篇文章主要介紹了Yii2數(shù)據(jù)庫(kù)操作常用方法,結(jié)合實(shí)例形式總結(jié)分析了Yii2常用的增刪查改及配置相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Yii2數(shù)據(jù)庫(kù)操作常用方法。分享給大家供大家參考,具體如下:

查詢:

// find the customers whose primary key value is 10
$customers = Customer::findAll(10);
$customer = Customer::findOne(10);
// the above code is equivalent to:
$customers = Customer::find()->where(['id' => 10])->all();
// find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findAll([10, 11, 12]);
$customers = Customer::find()->where(['IN','id',[10,11,12]])->all();
// the above code is equivalent to:
$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
// find customers whose age is 30 and whose status is 1
$customers = Customer::findAll(['age' => 30, 'status' => 1]);
// the above code is equivalent to:
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
// use params binding
$customers = Customer::find()->where('age=:age AND status=:status')->addParams([':age'=>30,':status'=>1])->all();
// use index
$customers = Customer::find()->indexBy('id')->where(['age' => 30, 'status' => 1])->all();
// get customers count
$count = Customer::find()->where(['age' => 30, 'status' => 1])->count();
// add addition condition
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->andWhere('score > 100')->orderBy('id DESC')->offset(5)->limit(10)->all();
// find by sql
$customers = Customer::findBySql('SELECT * FROM customer WHERE age=30 AND status=1 AND score>100 ORDER BY id DESC LIMIT 5,10')->all();

修改:

// update status for customer-10
$customer = Customer::findOne(10);
$customer->status = 1;
$customer->update();
// the above code is equivalent to:
Customer::updateAll(['status' => 1], 'id = :id',[':id'=>10]);

刪除:

// delete customer-10
Customer::findOne(10)->delete();
// the above code is equivalent to:
Customer::deleteAll(['status' => 1], 'id = :id',[':id'=>10]);

----------------使用子查詢----------------------

$subQuery = (new Query())->select('COUNT(*)')->from('customer');
// SELECT `id`, (SELECT COUNT(*) FROM `customer`) AS `count` FROM `customer`
$query = (new Query())->select(['id', 'count' => $subQuery])->from('customer');

----------------手寫SQL-----------------------

// select
$customers = Yii::$app->db->createCommand('SELECT * FROM customer')->queryAll();
// update
Yii::$app->db->createCommand()->update('customer',['status'=>1],'id=10')->execute();
// delete
Yii::$app->db->createCommand()->delete('customer','id=10')->execute();
//transaction
// outer
$transaction1 = $connection->beginTransaction();
try {
  $connection->createCommand($sql1)->execute();
  // internal
  $transaction2 = $connection->beginTransaction();
  try {
    $connection->createCommand($sql2)->execute();
    $transaction2->commit();
  } catch (Exception $e) {
    $transaction2->rollBack();
  }
  $transaction1->commit();
} catch (Exception $e) {
  $transaction1->rollBack();
}

---------------主從配置----------------------

[
  'class' => 'yii\db\Connection',
  // master
  'dsn' => 'dsn for master server',
  'username' => 'master',
  'password' => '',
  // slaves
  'slaveConfig' => [
    'username' => 'slave',
    'password' => '',
    'attributes' => [
      // use a smaller connection timeout
      PDO::ATTR_TIMEOUT => 10,
    ],
  ],
  'slaves' => [
    ['dsn' => 'dsn for slave server 1'],
    ['dsn' => 'dsn for slave server 2'],
    ['dsn' => 'dsn for slave server 3'],
    ['dsn' => 'dsn for slave server 4'],
  ],
]

更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP寫的簡(jiǎn)單數(shù)字驗(yàn)證碼實(shí)例

    PHP寫的簡(jiǎn)單數(shù)字驗(yàn)證碼實(shí)例

    下面小編就為大家?guī)?lái)一篇PHP寫的簡(jiǎn)單數(shù)字驗(yàn)證碼實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • discuz7 phpMysql操作類

    discuz7 phpMysql操作類

    MySql數(shù)據(jù)庫(kù)連接類,大家可以看下網(wǎng)上老手們用的什么方法,大家可以直接拿來(lái)用,但前提是大家能熟練的掌握的基礎(chǔ)上,這樣才能有所進(jìn)步。
    2009-06-06
  • PHP中abstract(抽象)、final(最終)和static(靜態(tài))原理與用法詳解

    PHP中abstract(抽象)、final(最終)和static(靜態(tài))原理與用法詳解

    這篇文章主要介紹了PHP中abstract(抽象)、final(最終)和static(靜態(tài)),結(jié)合實(shí)例形式分析了PHP中abstract(抽象)、final(最終)和static(靜態(tài))基本功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-06-06
  • php實(shí)現(xiàn)telnet功能示例

    php實(shí)現(xiàn)telnet功能示例

    這篇文章主要介紹了php實(shí)現(xiàn)telnet功能示例,需要的朋友可以參考下
    2014-04-04
  • PHP創(chuàng)建自己的Composer包方法

    PHP創(chuàng)建自己的Composer包方法

    這篇文章主要介紹了PHP創(chuàng)建自己的Composer包方法
    2018-04-04
  • thinkPHP中驗(yàn)證碼的簡(jiǎn)單使用方法

    thinkPHP中驗(yàn)證碼的簡(jiǎn)單使用方法

    這篇文章主要介紹了thinkPHP中驗(yàn)證碼的簡(jiǎn)單使用方法,涉及thinkPHP驗(yàn)證碼邏輯功能的實(shí)現(xiàn)與界面顯示的相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • PHP+Mysql分布式事務(wù)與解決方案深入理解

    PHP+Mysql分布式事務(wù)與解決方案深入理解

    這篇文章主要介紹了PHP+Mysql分布式事務(wù)與解決方案深入理解,有感興趣的同學(xué)可以學(xué)習(xí)下
    2021-02-02
  • PHP輸出九九乘法表代碼實(shí)例

    PHP輸出九九乘法表代碼實(shí)例

    這篇文章主要介紹了PHP輸出九九乘法表代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-03-03
  • PHP處理大量表單字段的便捷方法

    PHP處理大量表單字段的便捷方法

    這篇文章主要介紹了PHP處理大量表單字段的便捷方法,本文講解一種使用數(shù)組的方式來(lái)快速、方便的處理大量表單數(shù)據(jù),需要的朋友可以參考下
    2015-02-02
  • laravel7學(xué)習(xí)之無(wú)限級(jí)分類的最新實(shí)現(xiàn)方法

    laravel7學(xué)習(xí)之無(wú)限級(jí)分類的最新實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于laravel7學(xué)習(xí)之無(wú)限級(jí)分類的最新實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評(píng)論

贞丰县| 佛山市| 日土县| 集贤县| 德州市| 历史| 嫩江县| 洛宁县| 招远市| 镇雄县| 永善县| 湘乡市| 北安市| 城市| 通州区| 贡嘎县| 百色市| 三明市| 阿巴嘎旗| 永泰县| 长武县| 蓝田县| 麻栗坡县| 磴口县| 仁化县| 墨江| 白朗县| 巨鹿县| 灵武市| 金华市| 高陵县| 五河县| 彝良县| 通州市| 龙南县| 惠水县| 青海省| 无极县| 呼伦贝尔市| 富川| 宝应县|