久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

這篇文章主要介紹了Yii2數(shù)據(jù)庫操作常用方法,結(jié)合實(shí)例形式總結(jié)分析了Yii2常用的增刪查改及配置相關(guān)操作技巧,需要的朋友可以參考下

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

查詢:

// 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ù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

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

【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。

相關(guān)文檔推薦

這篇文章主要介紹了tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式簡單分析了mongoDB數(shù)據(jù)庫及thinkPHP5連接、查詢MongoDB數(shù)據(jù)庫的基本操作技巧,需要的朋友可以參考下
這篇文章主要介紹了thinkPHP5框架數(shù)據(jù)庫連貫操作之cache()用法,結(jié)合實(shí)例形式分析了thinkPHP5中緩存cache的應(yīng)用場景及連貫操作中cache的設(shè)置、更新、刪除等操作技巧,需要的朋友可以參考下
Yii2的rule用于對模型屬性進(jìn)行驗(yàn)證,scenario用戶定義不同場景下需要驗(yàn)證的模型,下面這篇文章主要給大家介紹了關(guān)于Yii2中場景(scenario)和驗(yàn)證規(guī)則(rule)的相關(guān)資料,文中通過示例代碼介
sqlite數(shù)據(jù)庫只用一個(gè)文件就ok,小巧方便,所以是一個(gè)非常不錯(cuò)的嵌入式數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于php利用封裝db類連接sqlite3的相關(guān)資料,文中通過示例代碼介紹的非常
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)轉(zhuǎn)換數(shù)據(jù)庫查詢結(jié)果數(shù)據(jù)到對應(yīng)類型的方法,涉及thinkPHP模型類操作及針對源碼文件的相關(guān)修改方法,需要的朋友可以參考下
最近在學(xué)習(xí)Laravel數(shù)據(jù)庫方面的內(nèi)容,發(fā)現(xiàn)了一些資料不錯(cuò)整理出來分享給大家,下面這篇文章主要給大家介紹了關(guān)于Laravel實(shí)現(xiàn)數(shù)據(jù)庫遷移與支持中文填充的相關(guān)資料,文中通過示例代碼
主站蜘蛛池模板: 亚洲第一视频网站 | 久久国产免费看 | 日韩欧美亚洲 | 在线观看免费毛片 | 午夜免费观看 | 91亚洲国产 | 国产精品日韩欧美一区二区三区 | 日韩色视频| 欧美一区免费 | 亚洲天天干| 午夜视频免费在线观看 | 热久久久 | 国产成人精品久久二区二区91 | www.久久精品视频 | 3级毛片| 国产成人在线播放 | 久久激情网 | 欧美爱爱视频网站 | 亚洲国产黄 | 男人的天堂视频网站 | 日韩精品在线一区 | 亚州中文字幕 | 久久久久久久国产精品 | 欧美成人黄色小说 | 国产精品色 | 国产黄色在线观看 | 成年人免费在线视频 | 日韩在线免费 | 91在线精品一区二区 | 在线观看成人精品 | 7777在线视频 | 午夜精品久久 | 亚洲精品一区二区三区中文字幕 | 四虎成人免费电影 | 一区二区三区在线免费看 | 国产精品福利视频 | 久久久久久99 | 黄色精品 | 欧美日产国产成人免费图片 | 精品乱码一区二区三四区 | 亚洲成年人免费网站 |