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 session应用实例 登录验证
Mar 16 PHP
PHP 截取字符串专题集合
Aug 19 PHP
php中使用exec,system等函数调用系统命令的方法(不建议使用,可导致安全问题)
Sep 07 PHP
PHP获取服务器端信息的方法
Nov 28 PHP
php+mysql查询优化简单实例
Jan 13 PHP
PHP实现获取客户端IP并获取IP信息
Mar 17 PHP
PHP学习笔记(二):变量详解
Apr 17 PHP
PHP中函数gzuncompress无法使用的解决方法
Mar 02 PHP
yii框架无限极分类的实现方法
Apr 08 PHP
PHP7新特性之抽象语法树(AST)带来的变化详解
Jul 17 PHP
PHP抽象类与接口的区别详解
Mar 21 PHP
Laravel框架集合用法实例浅析
May 14 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
IIS6的PHP最佳配置方法
2007/03/19 PHP
php实现的九九乘法口诀表简洁版
2014/07/28 PHP
CI配置多数据库访问的方法
2016/03/28 PHP
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例
2019/06/17 PHP
收藏一些不常用,但是有用的代码
2007/03/12 Javascript
JavaScript isArray()函数判断对象类型的种种方法
2010/10/11 Javascript
全面解析Bootstrap中transition、affix的使用方法
2016/05/30 Javascript
深入理解JavaScript中的call、apply、bind方法的区别
2016/05/30 Javascript
js拼接html字符串的注意事项
2016/10/13 Javascript
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound的解决方法
2017/01/19 Javascript
详解vee-validate的使用个人小结
2017/06/07 Javascript
Angularjs上传图片实例详解
2017/08/06 Javascript
vue-resource + json-server模拟数据的方法
2017/11/02 Javascript
vue 的点击事件获取当前点击的元素方法
2018/09/15 Javascript
JavaScript面向对象程序设计中对象的定义和继承详解
2019/07/29 Javascript
微信小程序登录对接Django后端实现JWT方式验证登录详解
2019/07/29 Javascript
JS实现图片幻灯片效果代码实例
2020/05/21 Javascript
基于python历史天气采集的分析
2019/02/14 Python
详解Python3之数据指纹MD5校验与对比
2019/06/11 Python
python实现xml转json文件的示例代码
2020/12/30 Python
详解HTML5中download属性的应用
2015/08/06 HTML / CSS
贝玲妃美国官方网站:Benefit美国
2016/08/28 全球购物
WWE美国职业摔角官方商店:WWE Shop
2018/11/15 全球购物
会计专业自我鉴定范文
2013/12/29 职场文书
应届实习生的自我评价范文
2014/01/05 职场文书
教师研修随笔感言
2014/01/23 职场文书
青奥会口号
2014/06/12 职场文书
2014年小学教师工作自我评价
2014/09/22 职场文书
2014党的群众路线教育实践活动总结材料
2014/10/31 职场文书
2014年平安夜寄语
2014/12/08 职场文书
病假证明模板
2015/06/19 职场文书
酒店开业主持词
2015/07/02 职场文书
开学第一周总结
2015/07/16 职场文书
三下乡活动心得体会
2016/01/23 职场文书
Python中字符串对象语法分享
2022/02/24 Python
apache虚拟主机配置的三种方式(小结)
2022/07/23 Servers