• 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();
    View Code

    插入

    批量插入

     $res = Yii::$app->db->createCommand()->batchInsert('exam_paper_answer', ['subject_id', 'answers', 'score', 'paper_id'], $answer_arr)->execute();
                   
    View Code

    更新

    User::updateAllCounters(['point' => 8],'id=1');
    User::updateAllCounters(['point' =>-8],'id=1');
    View Code

    事务

    案例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 yiidbException(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 yiidbException('请重新提交');
                    }
                }else{
                    throw new yiidbException('尚未做题');
                }
                $transaction->commit();
            } catch (yiidbException $e) {
                $transaction->rollBack();
                return ['code' => 2204, 'msg' => $e->getMessage()];
            }
    View Code

    两表连查  

    $model = Article::find()->joinWith(['type'])->select('new,t_name,article.t_id')->asArray()->all(); print_r($model);
    use yiidbQuery;
    $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

  • 相关阅读:
    Spring学习之声明式事物管理
    SSH整合所需jar
    Spring学习之AOP
    常见MyEclipse报错—— serialVersionUID的作用
    Spring学习之基本概念
    Java基础——I/O续
    Java基础——I/O
    Vim学习总结
    Java基础——异常处理
    Git 学习总结
  • 原文地址:https://www.cnblogs.com/huay/p/10642374.html
Copyright © 2020-2023  润新知