一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - Yii2數(shù)據(jù)庫操作常用方法小結(jié)

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

2021-05-14 20:51yhdsir PHP教程

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

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

查詢:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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();

修改:

?
1
2
3
4
5
6
// 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]);

刪除:

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

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

?
1
2
3
$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-----------------------

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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();
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
  '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'],
  ],
]

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产免费资源高清小视频在线观看 | 国产免费一区不卡在线 | 日本三级免费看 | 国产成人小视频在线观看 | 牛人国产偷窥女洗浴在线观看 | 日本视频在线免费看 | 国产一区二区免费不卡在线播放 | 精品午夜寂寞黄网站在线 | 国产资源在线视频 | 欧美国产精品 | 欧美久久热 | 日本人添下面的全过程 | 99热精品在线观看 | 欧美在线观看一区二区三 | 成人影院在线观看免费 | 大象传媒免费网址 | 青青青国产成人久久111网站 | 日韩成人精品 | 久久视频这里只精品99热在线观看 | 四虎国产精品视频免费看 | 欧美在线视频一区 | 污丝瓜视频 | 日韩一级片在线播放 | 日本不卡1卡2卡三卡网站二百 | 欧美xxxxx69 | 美女的让男人桶爽网站 | 欧美成黑人性猛交xxoo | 亚欧日韩 | chaopeng在线观看 | 性一交一无一伦一精一品 | 亚洲视频1 | 午夜国产精品影院在线观看 | 丝袜性爱 | 国产欧美日韩高清专区ho | 日韩精品免费看 | 国产乱子伦一区二区三区 | meyd—447佐山爱在线 | 免费一级毛片在线播放放视频 | 手机看片黄色 | 国产欧美另类久久精品91 | 亚洲美女人黄网成人女 |