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 相关文章推荐
PHP4之COOKIE支持详解
Oct 09 PHP
ZF等常用php框架中存在的问题
Jan 10 PHP
php删除与复制文件夹及其文件夹下所有文件的实现代码
Jan 23 PHP
php使用sql数据库 获取字段问题介绍
Aug 12 PHP
php-fpm配置详解
Feb 12 PHP
php生成固定长度纯数字编码的方法
Jul 09 PHP
详解PHP的Yii框架中组件行为的属性注入和方法注入
Mar 18 PHP
Yii+MYSQL锁表防止并发情况下重复数据的方法
Jul 14 PHP
利用php实现一周之内自动登录存储机制(cookie、session、localStorage)
Oct 31 PHP
PHP与jquery实时显示网站在线人数实例详解
Dec 02 PHP
safari下载文件自动加了html后缀问题
Nov 09 PHP
零基础php编程好学吗
Oct 11 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
使用dump函数,给php加断点测试
2013/06/25 PHP
WampServer下安装多个版本的PHP、mysql、apache图文教程
2015/01/07 PHP
常见PHP数据库解决方案分析介绍
2015/09/24 PHP
PHP实现补齐关闭的HTML标签
2016/03/22 PHP
php面向对象值单例模式
2016/05/03 PHP
php ZipArchive实现多文件打包下载实例
2019/10/31 PHP
深入浅析安装PhpStorm并激活的步骤详解
2020/09/17 PHP
iphone safari不支持position fixed的解决方法
2012/05/04 Javascript
图片Slider 带左右按钮的js示例
2013/08/30 Javascript
js中关于一个分号的崩溃示例
2013/11/11 Javascript
javascript中createElement的两种创建方式
2015/05/14 Javascript
AngularJS+Node.js实现在线聊天室
2015/08/28 Javascript
js获取时间精确到秒(年月日)
2016/03/16 Javascript
JavaScript中的this使用详解
2016/07/27 Javascript
js html实现计算器功能
2018/11/13 Javascript
小程序实现分类页
2019/07/12 Javascript
layui 上传文件_批量导入数据UI的方法
2019/09/23 Javascript
支付宝小程序实现省市区三级联动
2020/06/21 Javascript
JS如何操作DOM基于表格动态展示数据
2020/10/15 Javascript
python3序列化与反序列化用法实例
2015/05/26 Python
深入浅析ImageMagick命令执行漏洞
2016/10/11 Python
Python实现列表删除重复元素的三种常用方法分析
2017/11/24 Python
django富文本编辑器的实现示例
2019/04/10 Python
Python qrcode 生成一个二维码的实例详解
2020/02/12 Python
Python实现名片管理系统
2020/02/14 Python
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
JAVA代码查错题
2014/10/10 面试题
企业军训感言
2014/02/08 职场文书
结婚喜宴主持词
2014/03/14 职场文书
电子商务系毕业生自荐信
2014/05/29 职场文书
大四毕业生自荐书
2014/07/05 职场文书
初中学校对照检查材料
2014/08/19 职场文书
放飞理想演讲稿
2014/09/09 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
优秀新员工事迹材料
2019/05/13 职场文书