Yii2数据库操作的各种写法
-------------------------------ActiveRecord----------------------------------------
查询:
// find the customers whose primary key value is 10
$customers = Customer::findAll();
$customer = Customer::findOne(); // the above code is equivalent to:
$customers = Customer::find()->where(['id' => ])->all(); // find the customers whose primary key value is 10, 11 or 12.
$customers = Customer::findAll([, , ]);
$customers = Customer::find()->where(['IN','id',[,,]])->all(); // the above code is equivalent to:
$customers = Customer::find()->where(['id' => [, , ]])->all(); // find customers whose age is 30 and whose status is 1
$customers = Customer::findAll(['age' => , 'status' => ]); // the above code is equivalent to:
$customers = Customer::find()->where(['age' => , 'status' => ])->all(); // use params binding
$customers = Customer::find()->where('age=:age AND status=:status')->addParams([':age'=>,':status'=>])->all(); // use index
$customers = Customer::find()->indexBy('id')->where(['age' => , 'status' => ])->all(); // get customers count
$count = Customer::find()->where(['age' => , 'status' => ])->count(); // add addition condition
$customers = Customer::find()->where(['age' => , 'status' => ])->andWhere('score > 100')->orderBy('id DESC')->offset()->limit()->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();
$customer->status = ;
$customer->update(); // the above code is equivalent to:
Customer::updateAll(['status' => ], 'id = :id',[':id'=>]);
删除:
// delete customer-10
Customer::findOne()->delete(); // the above code is equivalent to:
Customer::deleteAll(['status' => ], 'id = :id',[':id'=>]);
--------------------------------使用子查询------------------------------------------
$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'=>],'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 => ,
],
], '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'],
],
]
更多详情参考:
- http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html
- http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html
- http://www.yiiframework.com/doc-2.0/yii-db-query.html
来源地址:http://www.getyii.com/topic/219
Yii2数据库操作的各种写法的更多相关文章
- Yii2 数据库操作汇总
对象操作 查询 //1.简单查询 $admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->fi ...
- yii2 数据库操作(转)
开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...
- yii2 数据库操作详解(转载)
开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...
- YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE
yii2安装后,连接数据库,必须要安装pdo_mysql扩展
- Yii2数据库操作 事务
Yii2 DAO http://blog.csdn.net/hzqghost/article/details/44116039
- Yii2数据库操作再总结
User::find()->all(); 此方法返回所有数据:User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->wh ...
- YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE[HY000] [2002] No such file or director
参考文章:https://blog.csdn.net/zqtsx/article/details/41845511 我的系统时Ubuntu18使用上面的方法时发现,没有MySQL.socket,然后谷 ...
- php框架内的数据库操作(微擎,tp,yii2)
微擎数据库操作 关键字 查询 pdo_get pdo_getcolumn pdo_getall pdo_getslice pdo_fetchcolumn pdo_fetchall 示例: array ...
- Yii 2.0 数据库操作总结
1. 概述 操作数据库有2种方式: DAO(data access object),不安全 ORM(onject relational mapping) 2. DAO Yii::app()->d ...
随机推荐
- Odoo 8,9,10 制造领料、入库 实践
Odoo12 已经支持在 同一个仓库内,使用 投入/产品 库位, 不必采用本文的方法 Odoo 设计在 仓库/库存 进行生产,也就是 在 仓库/库存 领料,产出, 例如 如果要实现一般 ...
- 【BIEE】分析的解析机制
今天使用BIEE时意外的发现个问题,BIEE在展示结果时候,是先进行排序,然后再展示.具体测试案例如下: 首先,存在如下数据: 在BIEE展示效果如下: 目前是根据O1,02,03,04,05,06, ...
- VM里面的桥接
今天我们谈一谈桥接模式,一般的时候,我用的是NAT模式,需要设置另一个虚拟网卡的IP地址,而不是借助本机的物理网卡. 编辑---虚拟网络编辑器 有些时候直接设置桥接到某个网卡可能会不成功,我们 ...
- &&与&符号区别
http://topic.csdn.net/u/20080915/16/f5125300-f69f-4da8-9c3a-a7458590553f.html && 与 &区别: ...
- Spring Cloud Zuul 网关的分布式系统中整合Swagger(转)和 zuul跨域访问问题
首先恭喜自己终于找对了努力的方向,很荣幸能在公司接触到微服务架构,也很高兴公司一个大佬哥们愿意带我,他技术确实很牛逼,我也很佩服他,前后端通吃,干了六年能有这样的水平.最近跟着在搞微服务架构,给我分配 ...
- 【MIG专项测试组】如何准确评测Android应用的流畅度?
转自 腾讯Bugly 叶方正,2008年加入腾讯,就职于无线研发部[专项测试组].曾经负责多个产品的性能优化工作,积累大量的移动终端平台优化以及评测经验. 怎样获取SM值? 前文我们分析了通过测量应用 ...
- PHP将多级目录打包成zip文件
最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行. 而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool a ...
- PHP性能:序——谈性能
PHP性能:序——谈性能 这里不谈PHP的性能和其他语言的性能,这里讨论PHP自身的性能问题. 性能是什么? 通俗的来讲,性能,就是在固定的环境下能做的事情的多少. 为什么要性能? 1.每一个软件或网 ...
- 利用github Pages和Jekyll搭建blog实践1
你必须要懂一点git和网页开发.安装了git,并且有github账户. github设计了Pages功能,允许用户自定义项目首页 github提供模板,允许站内生成网页,但也允许用户自己编写网页,然后 ...
- DB2恢复一例 SQL0928N
环境是AIX 6.1.DB2版本号9.7.0.7 首先查看db2主进程是否存在 ps -ef|grep db2sys 若不存在使用db2start打开数据库 备份介质为冷备数据源, cd 到介质所在文 ...