Yii2數(shù)據(jù)庫操作常用方法小結(jié)
本文實(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ì)有所幫助。
- Yii操作數(shù)據(jù)庫的3種方法
- 解析yii數(shù)據(jù)庫的增刪查改
- PHP的Yii框架中使用數(shù)據(jù)庫的配置和SQL操作實(shí)例教程
- Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
- Yii2.0高級框架數(shù)據(jù)庫增刪改查的一些操作
- Yii2——使用數(shù)據(jù)庫操作匯總(增刪查改、事務(wù))
- YII2數(shù)據(jù)庫查詢實(shí)踐
- Yii實(shí)現(xiàn)多數(shù)據(jù)庫主從讀寫分離的方法
- yii數(shù)據(jù)庫的查詢方法
- Yii操作數(shù)據(jù)庫實(shí)現(xiàn)動(dòng)態(tài)獲取表名的方法
- Yii 連接、修改 MySQL 數(shù)據(jù)庫及phpunit 測試連接
- Yii 框架使用數(shù)據(jù)庫(databases)的方法示例
相關(guān)文章
利用phpexcel把excel導(dǎo)入數(shù)據(jù)庫和數(shù)據(jù)庫導(dǎo)出excel實(shí)現(xiàn)
本文介紹利用phpexcel對數(shù)據(jù)庫數(shù)據(jù)導(dǎo)入excel(excel篩選)、導(dǎo)出excel,大家參考使用吧2014-01-01ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng)分析
這篇文章主要介紹了ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng),以注釋的形式詳細(xì)分析了入口文件設(shè)置時(shí)相關(guān)設(shè)置項(xiàng)的含義與設(shè)置技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12Laravel5中實(shí)現(xiàn)模糊匹配加多條件查詢功能的方法
這篇文章主要介紹了Laravel5中實(shí)現(xiàn)模糊匹配加多條件查詢功能的方法,結(jié)合實(shí)例形式分析了Laravel5多條件模糊查詢及相關(guān)封裝操作技巧,需要的朋友可以參考下2018-03-03