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常用查询的更多相关文章

  1. YII2常用知识点总结

    YII2常用知识点总结 (一)总结性语句 (1)经常看看yii源码比如vendor\yiisoft\yii2\web这个目录(很重要)下的文件中的方法(这些文件中的公共方法,大致看了下基本上都可以通过 ...

  2. MySQL - 常用命令及常用查询SQL

    常用查询SQL #查看临时目录 SHOW VARIABLES LIKE '%tmp%'; #查看当前版本 SELECT VERSION(); 常用命令 #查看当前版本,终端下未进入mysql mysq ...

  3. Oracle DBA常用查询

    Oracle DBA常用查询 –1. 查询系统所有对象select owner, object_name, object_type, created, last_ddl_time, timestamp ...

  4. Solr常用查询语法笔记

    1.常用查询 q - 查询字符串,这个是必须的.如果查询所有*:* ,根据指定字段查询(Name:张三 AND Address:北京) fq - (filter query)过虑查询,作用:在q查询符 ...

  5. 23个MySQL常用查询语句

    23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...

  6. BOM/ROUTING/PO/WIP等模块常用查询

    常用查询scripts /*bom*/ select p_item.segment1,c_item.segment1,bic.COMPONENT_QUANTITY,bic.COMPONENT_YIEL ...

  7. SqlSugar常用查询实例-拉姆达表达式

    SqlSugar支持拉姆达表达式查询,匿名对象参数等,相对还是比较方便好用的. 一.查询列表: //查询列表 SqlSugarClient db = SugarContext.GetInstance( ...

  8. oracle数据库常用查询一

    oracle数据库常用查询一 sqlplus / as sysdba; 或sqlplus sys/密码 as sysdba;两者都是以sys登录.conn scott/tiger@orcl; conn ...

  9. ORACLE数据库常用查询二

    ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...

随机推荐

  1. 学习DButils笔记

    DBUtills: *********************** 1:创建对象:QueryRunner的对象,其中创建的方式有两种: ①QueryRunner qr = new QueryRunne ...

  2. spring boot + vue + element-ui全栈开发入门——前端编辑数据对话框

     需求 1.点击“添加”按钮,弹出录入数据的对话框窗口,并录入数据,如果数据有误则不允许提交.数据填写完毕后,点击“保存”按钮,调用http协议提交数据,提交完毕刷新页面数据.点击“取消”按钮关闭对话 ...

  3. js 字符串操作

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  4. Makefile 编译时虽然加上了-g 选项 但是还是无法调试

    make 编译时默认的命令是all,不能写成其他的

  5. 服务器告警其一:硬盘raid问题

    问题描述 服务器一直间断发出告警音,但是根据raid类型的不同有一定可能进入系统. 问题详情 在LSI Mega Webbios自检之后系统开始出现告警音. 在Lsi Mega Webbios的ini ...

  6. vue配置手机通过IP访问电脑开发环境

    vue配置手机通过IP访问电脑开发环境config/index.js// Various Dev Server settings host: '0.0.0.0', // can be overwrit ...

  7. Linux的DNS配置1-DNS入门

    1.DNS简介 1)什么是“DNS”? DNS,简单地说,就是Domain Name System,翻成中文就是“域名系统” 2)DNS有什么用途? 在一个TCP/IP架构的网络(例如Internet ...

  8. leetcode 最后一个单词的长度 python

    class Solution: def lengthOfLastWord(self, s): """ :type s: str :rtype: int "&qu ...

  9. 使用go-template自定义kubectl get输出

    kubectl get相关资源,默认输出为kubectl内置,一般我们也可以使用-o json或者-o yaml查看其完整的资源信息.但是很多时候,我们需要关心的信息并不全面,因此我们需要自定义输出的 ...

  10. Bugku-CTF之备份是个好习惯

    Day17 备份是个好习惯 听说备份是个好习惯 http://123.206.87.240:8002/web16/