zoukankan      html  css  js  c++  java
  • yii2表关联实例

    yii2表关联

    1.两张表关联,以“商品表关联品牌表”为例

    控制器中:

    $goods_model=new Goods();

    $goods_info=$goods_model::find()->joinWith('brand')->all();

    商品模型中:

    //关联品牌表
        public function getbrand(){
            return $this->hasMany(Brand::className(), ['brand_id' => 'goods_brand_id']);
        }

    2.三张表关联,以“商品表关联品牌表和分类表”为例

    控制器中:

    $goods_model=new Goods();

    $goods_info=$goods_model::find()->joinWith('brand','category')->orderBy('goods.goods_id desc');

    商品模型中:

    //关联品牌表

    public function getbrand(){

          return $this->hasMany(Brand::className(),['brand_id'=>'goods_brand_id']);

    }

    //关联类别表

    public function getcategory(){

        return $this->hasMany(Category::className(),['category_id'=>'goods_category_id']);

    }

  • 相关阅读:
    【转载】CSS的inline、block与inline-block
    MySQL常用语法
    JS模态对话框
    CS3常用属性手记
    画布常用手记
    CSS属性常用手记
    H5试题
    window对象常用手记
    js对象常用手记
    常用DOM对象手记
  • 原文地址:https://www.cnblogs.com/xlz307/p/3852794.html
Copyright © 2011-2022 走看看