当前位置:Gxlcms > php框架 > Yii2数据库操作常用方法小结

Yii2数据库操作常用方法小结

时间:2021-07-01 10:21:17 帮助过:45人阅读

本文实例讲述了Yii2数据库操作常用方法。分享给大家供大家参考,具体如下:

查询:

  1. // find the customers whose primary key value is 10
  2. $customers = Customer::findAll(10);
  3. $customer = Customer::findOne(10);
  4. // the above code is equivalent to:
  5. $customers = Customer::find()->where(['id' => 10])->all();
  6. // find the customers whose primary key value is 10, 11 or 12.
  7. $customers = Customer::findAll([10, 11, 12]);
  8. $customers = Customer::find()->where(['IN','id',[10,11,12]])->all();
  9. // the above code is equivalent to:
  10. $customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
  11. // find customers whose age is 30 and whose status is 1
  12. $customers = Customer::findAll(['age' => 30, 'status' => 1]);
  13. // the above code is equivalent to:
  14. $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
  15. // use params binding
  16. $customers = Customer::find()->where('age=:age AND status=:status')->addParams([':age'=>30,':status'=>1])->all();
  17. // use index
  18. $customers = Customer::find()->indexBy('id')->where(['age' => 30, 'status' => 1])->all();
  19. // get customers count
  20. $count = Customer::find()->where(['age' => 30, 'status' => 1])->count();
  21. // add addition condition
  22. $customers = Customer::find()->where(['age' => 30, 'status' => 1])->andWhere('score > 100')->orderBy('id DESC')->offset(5)->limit(10)->all();
  23. // find by sql
  24. $customers = Customer::findBySql('SELECT * FROM customer WHERE age=30 AND status=1 AND score>100 ORDER BY id DESC LIMIT 5,10')->all();

修改:

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

删除:

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

----------------使用子查询----------------------

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

----------------手写SQL-----------------------

  1. // select
  2. $customers = Yii::$app->db->createCommand('SELECT * FROM customer')->queryAll();
  3. // update
  4. Yii::$app->db->createCommand()->update('customer',['status'=>1],'id=10')->execute();
  5. // delete
  6. Yii::$app->db->createCommand()->delete('customer','id=10')->execute();
  7. //transaction
  8. // outer
  9. $transaction1 = $connection->beginTransaction();
  10. try {
  11. $connection->createCommand($sql1)->execute();
  12. // internal
  13. $transaction2 = $connection->beginTransaction();
  14. try {
  15. $connection->createCommand($sql2)->execute();
  16. $transaction2->commit();
  17. } catch (Exception $e) {
  18. $transaction2->rollBack();
  19. }
  20. $transaction1->commit();
  21. } catch (Exception $e) {
  22. $transaction1->rollBack();
  23. }

---------------主从配置----------------------

  1. [
  2. 'class' => 'yii\db\Connection',
  3. // master
  4. 'dsn' => 'dsn for master server',
  5. 'username' => 'master',
  6. 'password' => '',
  7. // slaves
  8. 'slaveConfig' => [
  9. 'username' => 'slave',
  10. 'password' => '',
  11. 'attributes' => [
  12. // use a smaller connection timeout
  13. PDO::ATTR_TIMEOUT => 10,
  14. ],
  15. ],
  16. 'slaves' => [
  17. ['dsn' => 'dsn for slave server 1'],
  18. ['dsn' => 'dsn for slave server 2'],
  19. ['dsn' => 'dsn for slave server 3'],
  20. ['dsn' => 'dsn for slave server 4'],
  21. ],
  22. ]

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

人气教程排行