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


Posted in PHP onMay 04, 2017

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

查询:

// 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'],
  ],
]

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

PHP 相关文章推荐
php 论坛采集程序 模拟登陆,抓取页面 实现代码
Jul 09 PHP
解析用PHP读写音频文件信息的详解(支持WMA和MP3)
May 10 PHP
深入PHP操作MongoDB的技术总结
Jun 02 PHP
Window下PHP三种运行方式图文详解
Jun 11 PHP
php打印一个边长为N的实心和空心菱型的方法
Mar 02 PHP
php实现插入排序
Mar 29 PHP
php实现用手机关闭计算机(电脑)的方法
Apr 22 PHP
FleaPHP框架数据库查询条件($conditions)写法总结
Mar 19 PHP
PHP实现将标点符号正则替换为空格的方法
Aug 09 PHP
php实现微信公众号企业转账功能
Oct 01 PHP
解决laravel中日志权限莫名变成了root的问题
Oct 17 PHP
PHP控制循环操作的时间
Apr 01 PHP
Yii2中添加全局函数的方法分析
May 04 #PHP
Yii2表单事件之Ajax提交实现方法
May 04 #PHP
PHP经典实用正则表达式小结
May 04 #PHP
PHP实现的简单异常处理类示例
May 04 #PHP
PHP基于新浪IP库获取IP详细地址的方法
May 04 #PHP
PHP 无限级分类
May 04 #PHP
PHP实现中国公民身份证号码有效性验证示例代码
May 03 #PHP
You might like
PHP中的类型约束介绍
2015/05/11 PHP
php数组合并与拆分实例分析
2015/06/12 PHP
highchart数据源纵轴json内的值必须是int(详解)
2017/02/20 PHP
浅析PHP开发规范
2018/02/05 PHP
ImageZoom 图片放大镜效果(多功能扩展篇)
2010/04/14 Javascript
JS对HTML标签select的获取、添加、删除操作
2013/10/17 Javascript
js 通用订单代码
2013/12/23 Javascript
对js关键字命名的疑问介绍
2014/04/25 Javascript
js实现一个链接打开两个链接地址的方法
2015/05/12 Javascript
JavaScript中的pow()方法使用详解
2015/06/15 Javascript
jquery使用ul模拟select实现表单美化的方法
2015/08/18 Javascript
JS简单实现城市二级联动选择插件的方法
2015/08/19 Javascript
基于BootStrap Metronic开发框架经验小结【四】Bootstrap图标的提取和利用
2016/05/12 Javascript
基于Vue.js实现数字拼图游戏
2016/08/02 Javascript
Vue.js每天必学之数据双向绑定
2016/09/05 Javascript
JavaScript 是什么意思
2016/09/22 Javascript
表单元素值获取方式js及java方式的简单实例
2016/10/15 Javascript
vue2滚动条加载更多数据实现代码
2017/01/10 Javascript
微信小程序实战之自定义toast(6)
2017/04/18 Javascript
详解如何使用PM2将Node.js的集群变得更加容易
2017/11/15 Javascript
JavaScript原生实现观察者模式的示例
2017/12/15 Javascript
AngularJS监听ng-repeat渲染完成的两种方法
2018/01/16 Javascript
JavaScript实现仿Clock ISO时钟
2018/06/29 Javascript
改进 JavaScript 和 Rust 的互操作性并深入认识 wasm-bindgen 组件
2019/07/13 Javascript
python解析html提取数据,并生成word文档实例解析
2018/01/22 Python
numpy 进行数组拼接,分别在行和列上合并的实例
2018/05/08 Python
如何在Python中实现goto语句的方法
2019/05/18 Python
Django url,从一个页面调到另个页面的方法
2019/08/21 Python
Java Spring项目国际化(i18n)详细方法与实例
2020/03/20 Python
英国创新设计文具、卡片和礼品包装网站:Paperchase
2018/07/14 全球购物
Foot Locker英国官网:美国知名运动产品零售商
2019/02/21 全球购物
药剂学专业应届生自荐信
2013/09/29 职场文书
医院总经理岗位职责
2014/02/04 职场文书
工商企业管理专业自荐信范文
2014/04/12 职场文书
普通党员个人对照检查材料
2014/09/18 职场文书
2014年大学团支部工作总结
2014/12/02 职场文书