您现在的位置是:网站首页>软件开发>开发终端>yii2yii2

yii2 关联查询总结

风口下的猪2023-03-15yii2

简介

1 join 方式 直接关联查询

$goodsId = BargainGoods::find()->alias('bg')
                        ->where(['bg.mall_id' => $this->mall->id, 'bg.is_delete' => 0])
                        ->leftJoin(['g' => Goods::tableName()], 'g.id = bg.goods_id')
                        ->andWhere(['g.goods_warehouse_id' => $goodsWarehouseId])
                        ->select(['bg.id']);

2  with方式

(1)方法一:模型添加关联关系法

$detail = Mch::find()->where([
                'id' => \Yii::$app->user->identity->mch_id ?: $this->id,
                'mall_id' => \Yii::$app->mall->id,
                'is_delete' => 0,
            ])->with('user.userInfo', 'mchUser', 'store', 'category','store.tradeArea','store.tradeAreaList')->asArray()->one();

 public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
    public function getMchUser()
    {
        return $this->hasOne(User::className(), ['mch_id' => 'id']);
    }
    public function getStore()
    {
        return $this->hasOne(Store::className(), ['mch_id' => 'id']);
    }
    public function getCategory()
    {
        return $this->hasOne(MchCommonCat::className(), ['id' => 'mch_common_cat_id']);
    }

(2)
->with(['goodsSku' => function(ActivityQuery $query) {
$query->with(['category']);
}]);





很赞哦! (0)

  • 软件开发
  • 素质要求
  • 计算机基础
  • 架构
  • 安全
  • 性能
  • 运维
  • 尾页
  • 数据库
  • 开发终端
  • 语言基础
  • 项目管理
  • 产品设计
  • 系统
  • 工作规范
  • 计算机网络
  • 前端技术栈
  • 数据结构
  • 计算机组成原理
  • 后端技术栈
  • 性能优化
  • 安全设计
  • 常见模块
  • 计算机操作系统
  • 服务器
  • python
  • MySQL
  • thinkphp
  • PHP
  • Java
  • JavaScript
  • Windows
  • Linux
  • 特效
  • indexedDB
  • vue
  • 淘宝联盟
  • Ionic
  • Angular
  • 微信小程序
  • 支付宝小程序
  • uni-app
  • css/sass/less
  • 支付
  • socket
  • 爬虫
  • web性能优化
  • 消息推送
  • CVM
  • sqlite
  • Redis
  • 前端基础
  • 基础
  • element
  • Nginx
  • yii2
  • /ponder/index.php/index/catelist/catelist/cateid/10.html

    标签云

    站点信息

    • 文章统计:528篇
    • 移动端访问:扫码进入SQ3R