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 相关文章推荐
yii框架中的Url生产问题小结
Jan 16 PHP
php登陆页的密码处理方式分享
Oct 14 PHP
PHP采用curl模仿用户登陆新浪微博发微博的方法
Nov 07 PHP
php实现用于验证所有类型的信用卡类
Mar 24 PHP
PHP简单的MVC框架实现方法
Dec 01 PHP
修改WordPress中文章编辑器的样式的方法详解
Dec 15 PHP
PHP简单遍历对象示例
Sep 28 PHP
PHP正则匹配操作简单示例【preg_match_all应用】
Jul 10 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
Jun 20 PHP
JS操作XML中DTD介绍及使用方法分析
Jul 04 PHP
安装docker和docker-compose实例详解
Jul 30 PHP
关于laravel框架中的常用目录路径函数
Oct 23 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
关于时间计算的结总
2006/12/06 PHP
ThinkPHP模板IF标签用法详解
2014/07/01 PHP
php查找字符串出现次数的方法
2014/12/01 PHP
php解析mht文件转换成html的实例
2017/03/13 PHP
php静态成员方法和静态的成员属性的使用方法
2017/10/26 PHP
thinkPHP3.2.3实现阿里大于短信验证的方法
2018/06/06 PHP
TP5(thinkPHP框架)实现后台清除缓存功能示例
2019/05/29 PHP
javascript关于继承的用法汇总
2014/12/20 Javascript
JavaScript实现Base64编码转换
2016/04/23 Javascript
Angular+Bootstrap+Spring Boot实现分页功能实例代码
2017/07/21 Javascript
深入剖析Node.js cluster模块
2018/05/23 Javascript
Vue前后端不同端口的实现方法
2018/09/19 Javascript
Vue.js实现备忘录功能
2019/06/26 Javascript
Python脚本获取操作系统版本信息
2016/12/17 Python
从头学Python之编写可执行的.py文件
2017/11/28 Python
python使用itchat库实现微信机器人(好友聊天、群聊天)
2018/01/04 Python
Tensorflow 同时载入多个模型的实例讲解
2018/07/27 Python
python3.7.0的安装步骤
2018/08/27 Python
pycharm 取消默认的右击运行unittest的方法
2018/11/29 Python
Python3 安装PyQt5及exe打包图文教程
2019/01/08 Python
python 默认参数相关知识详解
2019/09/18 Python
python数据分析工具之 matplotlib详解
2020/04/09 Python
Python爬虫如何破解JS加密的Cookie
2020/11/19 Python
python 实现简易的记事本
2020/11/30 Python
Python的轻量级ORM框架peewee使用教程
2021/02/05 Python
美国体育用品商店:Paragon Sports
2017/10/08 全球购物
英国灯具和灯泡网上商店:Lights.co.uk
2018/02/02 全球购物
Delphi CS笔试题
2014/01/04 面试题
自主招生自荐信
2013/12/08 职场文书
优秀部门获奖感言
2014/02/14 职场文书
学习党的群众路线教育实践活动心得体会
2014/03/01 职场文书
早读课迟到检讨书
2014/09/25 职场文书
审美与表现自我评价
2015/03/09 职场文书
唱歌比赛拉拉队口号
2015/12/25 职场文书
公务员学习中国梦心得体会
2016/01/05 职场文书
php png失真的原因及解决办法
2021/11/17 PHP