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 ...
随机推荐
- 使用Gulp定制前端开发环境
1.安装package.json中依赖了的组件 npm install 2.来到本地路径,创建工程配置文件 npm init 3.本地安装gulp npm install gulp --save-de ...
- PHP使用微软认知服务Face API
下面主要介绍基于PHP语言,基于guzzle类库,调用微软最新推出的认知服务:人脸识别. 实验环境: IDE:Eclipse for PHP Developers Version: Neon.1 Re ...
- Github上的PHP开源资源汇总
依赖管理 ——用于依赖管理的包和框架 Composer/Packagist : 一个包和依赖管理器 Composer Installers: 一个多框架Composer库安装器 Pickle: 可以 ...
- PHP面试题及答案解析(7)—Linux系统命令
1.请解释下列10个shell命令的用途.top.ps.mv.find.df.cat.chmod.chgrp.grep.wc top:该命令提供了实时对系统处理器状态的监控,它能够实时显示系统中各个进 ...
- POJ 3126 Prime Path (BFS+剪枝)
题目链接:传送门 题意: 给定两个四位数a.b,每次能够改变a的随意一位.而且确保改变后的a是一个素数. 问最少经过多少次改变a能够变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是假设这个 ...
- Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java
Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java 1. 要实现的功能1 2. ahk是个好东东..能启动了...1 3. exe4j vs nativej1 4. 2 ...
- Atitit. Atiposter 发帖机版本历史 编年史
Atitit. Atiposter 发帖机版本历史 编年史 V1 初步实现sina csdn cnblogs V2 实现qzone sohu 的发帖功能 顺便重构接口实现分离 V3多文件循环发帖 ...
- jar包解压与打包
首先感谢大神的指导:https://blog.csdn.net/mr_pang/article/details/47028921 1.首先准备一个能运行的jar文件,我们使用第三方解压工具进行解压wi ...
- nginx 内置变量大全
HTTP核心模块支持一些内置变量,变量名与apache里的对应.比如 $http_user_agent,$http_cookie等表示HTTP请求信息的变量.更多变量:$args, 请求中的参数; $ ...
- MIC的异步传输
关于signal和wait,属于异步传输的语法,即CPU端无需等待offload语句返回,即可异步运行下面的代码.一般用于启动MIC代码段后,并发执行CPU代码,达到同步执行的目的.另外一种用法是使用 ...