yii2常用查询
yii curd操作
查询
复合查询
$data = $query->orderBy('apply.created_at desc')
->leftJoin('party','party.id=apply.party_id')
->select('apply.*,party.site')
->offset($pagination->offset)
->limit($pagination->limit)
->where(['apply.user_id' => $user_id])
->asArray()
->all();
$user_info = MzCaseGroup::find()->alias('g')
->select(['i.*','g.create_time as times'])
->leftJoin('mz_insurance_user i','`g`.`user_id` = `i`.`user_id`')
->where(['group_id'=>$id])
->asArray()
->one();
插入
批量插入
$res = Yii::$app->db->createCommand()->batchInsert('exam_paper_answer', ['subject_id', 'answers', 'score', 'paper_id'], $answer_arr)->execute();
更新
User::updateAllCounters(['point' => 8],'id=1');
User::updateAllCounters(['point' =>-8],'id=1');
事务
案例1
$transaction = Yii::$app->db->beginTransaction();
try {
//存入数据
$exam_paper = new ExamPaper();
$exam_paper->exam_id = +$answer['exam_id'];
$exam_paper->partner_id = $user->id;
$exam_paper->start_time = $answer['start_time'];
$exam_paper->end_time = date("Y-m-d H:i:s");
$exam_paper->score = $total_score;
$exam_paper->status = $status;
if (!$exam_paper->save()) {
throw new yii\db\Exception(current($exam_paper->getFirstErrors()));
}
$paper_id = $exam_paper->id;
foreach ($answer_arr as $k => $v) {
array_push($answer_arr[$k] ,$paper_id);
}
if($answer_arr){
//批量存入题目
$res = Yii::$app->db->createCommand()->batchInsert('exam_paper_answer', ['subject_id', 'answers', 'score', 'paper_id'], $answer_arr)->execute();
if (!$res) {
throw new yii\db\Exception('请重新提交');
}
}else{
throw new yii\db\Exception('尚未做题');
}
$transaction->commit();
} catch (yii\db\Exception $e) {
$transaction->rollBack();
return ['code' => 2204, 'msg' => $e->getMessage()];
}
查
两表连查
$model = Article::find()->joinWith(['type'])->select('new,t_name,article.t_id')->asArray()->all();
print_r($model);
use yii\db\Query;
$top10 = (new Query())
->from('article')
->select(['created_at','title'])
->groupBy(['created_at'])
->orderBy('id DESC')
->limit(10)
->all(); //参数绑定,查询操作
$sql='SELECT c.id,c.name,c.phone,c.age,c.sex,c.birthday,a.parent_id,a.camp_id,a.is_uppic,a.is_comment FROM `'.ChildrenApply::getTableSchema()->name.'` AS a left JOIN `'.Children::getTableSchema()->name.'` AS c ON c.`id`=a.`children_id` WHERE a.`parent_id`=:parent_id AND a.`camp_id`=:camp_id and a.status=1';
$res=Yii::$app->db->createCommand($sql)->bindValues([':parent_id' => $parent_id,':camp_id' => $camp_id])->queryAll();
插入操作
$res= Yii::$app->db->createCommand()->batchInsert(Children::tableName(), $dbKey, $dbValue)->execute();
//自增和自减
//关联分页查询
打印sql
echo $query->createCommand()->getRawSql();die;
参考https://www.yiichina.com/tutorial/95
yii2常用查询的更多相关文章
- YII2常用知识点总结
YII2常用知识点总结 (一)总结性语句 (1)经常看看yii源码比如vendor\yiisoft\yii2\web这个目录(很重要)下的文件中的方法(这些文件中的公共方法,大致看了下基本上都可以通过 ...
- MySQL - 常用命令及常用查询SQL
常用查询SQL #查看临时目录 SHOW VARIABLES LIKE '%tmp%'; #查看当前版本 SELECT VERSION(); 常用命令 #查看当前版本,终端下未进入mysql mysq ...
- Oracle DBA常用查询
Oracle DBA常用查询 –1. 查询系统所有对象select owner, object_name, object_type, created, last_ddl_time, timestamp ...
- Solr常用查询语法笔记
1.常用查询 q - 查询字符串,这个是必须的.如果查询所有*:* ,根据指定字段查询(Name:张三 AND Address:北京) fq - (filter query)过虑查询,作用:在q查询符 ...
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- BOM/ROUTING/PO/WIP等模块常用查询
常用查询scripts /*bom*/ select p_item.segment1,c_item.segment1,bic.COMPONENT_QUANTITY,bic.COMPONENT_YIEL ...
- SqlSugar常用查询实例-拉姆达表达式
SqlSugar支持拉姆达表达式查询,匿名对象参数等,相对还是比较方便好用的. 一.查询列表: //查询列表 SqlSugarClient db = SugarContext.GetInstance( ...
- oracle数据库常用查询一
oracle数据库常用查询一 sqlplus / as sysdba; 或sqlplus sys/密码 as sysdba;两者都是以sys登录.conn scott/tiger@orcl; conn ...
- ORACLE数据库常用查询二
ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...
随机推荐
- 【Python学习】iterator 迭代器小练习
http://anandology.com/python-practice-book/iterators.html Problem 1: Write an iterator class revers ...
- CentOS7中PPTP的配置
最近做各种vpn,记录一下pptp的流程 1.准备 #yum install -y perl ppp iptables //centos默认安装了iptables和ppp 2.安装pptpd #y ...
- log4j2 标签解析
根节点Configuration有两个属性:status和monitorinterval,有两个子节点:Appenders和Loggers. status用来指定log4j本身的打印日志的级别. mo ...
- python itertools 模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...
- css基础教程
css规则有两个主要部分构成:选择器,以及一条或多条声明. 值的不同写法和单位: 可以使用十六进制设置颜色值:#ff0000; 为节约字节,使用css缩写形式:#f00: 类选择器:以一个点号显示. ...
- Hadoop Mapreduce中shuffle 详解
MapReduce 里面的shuffle:描述者数据从map task 输出到reduce task 输入的这段过程 Shuffle 过程: 首先,map 输出的<key,value > ...
- JVM内存回收机制——哪些内存需要被回收(JVM学习系列2)
上一篇文章中讨论了Java内存运行时的各个区域,其中程序计数器.虚拟机栈.本地方法栈随线程生灭,且创建时需要多少内存,基本上在译期间就决定的了,所以在内存回收时无需特殊的关注.而堆和方法区则不同,首先 ...
- mysql,jdbc、连接池
show processlist; select * from information_schema.processlist; Command: The type of command the thr ...
- 修改tomcatlog输出等级
/usr/local/tomcat2/webapps/digital/WEB-INF/classes/log4j.properties log4j.properties配置如下(配置是转载): log ...
- 使用OGG添加唯一标识字段到目标表
利用GoldenGate,可以获取到变更记录在源端对应的redo日志序号,redo中的地址RBA,如果源端是RAC,还可以拿到源端节点的编号,通过这3个值,可以定位该变更记录的唯一性. 这些信息,在G ...