zoukankan      html  css  js  c++  java
  • yii2使用小知识(连续补充)

    1,打印ar或者query的原始sql:

    $query = (new yiidbQuery())
    ->select(['a.username','b.item_name'])
    ->from(['a' => 'user'])
    ->leftJoin(['b' => 'auth_assignment'], 'a.id = b.user_id')
    ->where(['a.username' => 'admin'])
    ->andWhere(['between', 'a.created_at', 1467626063, 1467626064])
    ->limit(10);

    // get the AR raw sql in YII2

    $commandQuery = clone $query;

    echo $commandQuery->createCommand()->getRawSql();

    注意$query不能带all,find() 返回值类型是 yiidbActiveQuery, find()->all() 返回的就是数组了。

    $users = $query->all();可在同级结构获取多表数据

    2,class User {

    public function getUsergroup()
    {
    return $this->hasOne(AuthAssignment::className(), ['user_id' => 'id']);
    }

    }

    $model = User::find()->joinWith('usergroup')

    ->where(['auth_assignment.item_name'=>'超级管理员'])->one();

    访问$model ->usergroup->item_name得到关联表数据

    ->select(['user.username','auth_assignment.item_name'])无法获取item_name,不可在同级结构获取多表数据

    3,

    $a= User::find()
    ->select(['user.username', 'b.item_name'])
    ->leftJoin(['b' => 'auth_assignment'], 'user.id = b.user_id')
    ->andWhere(['user.username' => 'admin'])
    ->andWhere(['between', 'user.created_at', 1467626063, 1467626064])
    ->limit(10)->all();

    同样的,无法得到item_name的值,但是可以第一次就用andWhere替代where可得到正确结果

  • 相关阅读:
    mybatis-plus学习
    代码规范系列
    Error:Abnormal build process termination:
    SpringBoot常用注解
    git深度学习
    spring中创建bean的方式
    jenkis构建“ERROR: Error fetching remote repo ‘origin’“
    tensor2tensor安装bug修复
    Swin-Transformer代码工程进行物体检测
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/huanxiyun/p/5699081.html
Copyright © 2011-2022 走看看