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中3des加密代码(完全与.net中的兼容)
Aug 02 PHP
解析yii数据库的增删查改
Jun 20 PHP
PHP中的函数-- foreach()的用法详解
Jun 24 PHP
三种php连接access数据库方法
Nov 11 PHP
phpQuery让php处理html代码像jQuery一样方便
Jan 06 PHP
PHP设计模式之适配器模式代码实例
May 11 PHP
php使用glob函数遍历文件和目录详解
Sep 23 PHP
遍历指定目录,并存储目录内所有文件属性信息的php代码
Oct 28 PHP
PHP实现二维数组去重功能示例
Jan 12 PHP
php实现图片按比例截取的方法
Feb 06 PHP
PHP中Static(静态)关键字功能与用法实例分析
Apr 05 PHP
PHP7新增函数
Mar 09 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
星际争霸任务指南——人族
2020/03/04 星际争霸
?繁体转换的class
2006/10/09 PHP
php中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
php数组排序usort、uksort与sort函数用法
2014/11/17 PHP
php获取指定数量随机字符串的方法
2017/02/06 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
网页右键ie不支持event.preventDefault和event.returnValue (需要加window)
2013/02/22 Javascript
我的Node.js学习之路(三)--node.js作用、回调、同步和异步代码 以及事件循环
2014/07/06 Javascript
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
javascript实现相同事件名称,不同命名空间的调用方法
2015/06/26 Javascript
javascript实现抽奖程序的简单实例
2016/06/07 Javascript
jQuery实现的图片轮播效果完整示例
2016/09/12 Javascript
第一次接触Bootstrap框架
2016/10/24 Javascript
JS中substring与substr的用法
2016/11/16 Javascript
详解用webpack2.0构建vue2.0超详细精简版
2017/04/05 Javascript
Node.js 实现简单的无侵入式缓存框架的方法
2019/07/21 Javascript
[37:21]完美世界DOTA2联赛PWL S2 Inki vs Magma 第二场 11.22
2020/11/24 DOTA
使用Python编写类UNIX系统的命令行工具的教程
2015/04/15 Python
在Django中使用Sitemap的方法讲解
2015/07/22 Python
python和ruby,我选谁?
2017/09/13 Python
Windows环境下python环境安装使用图文教程
2018/03/13 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
Django 实现购物车功能的示例代码
2018/10/08 Python
python实现UDP协议下的文件传输
2020/03/20 Python
Python数据可视化图实现过程详解
2020/06/12 Python
aec加密 php_php aes加密解密类(兼容php5、php7)
2021/03/14 PHP
安全环保标语
2014/06/09 职场文书
学校百日安全生产活动总结
2014/07/05 职场文书
建筑工程造价专业自荐信
2014/07/08 职场文书
2014公安机关纪律作风整顿思想汇报
2014/09/13 职场文书
2014年党员整改措施范文
2014/09/21 职场文书
2014年保育员工作总结
2014/12/02 职场文书
2015年村党支部工作总结
2015/04/30 职场文书
mysql查询的控制语句图文详解
2021/04/11 MySQL
详解pytorch创建tensor函数
2022/03/22 Python