yii2 joinwith简单使用

首先建立是三张表

国家表                                                               城市表                                        用户表

customer.php

public function getCountry()
{
    return $this->hasOne(Country2::className(),['user_id'=>'id'])->with('city');
//getCity在country2.php模型中,可以用with带过来
}

country2.php

 public function getCity()
    {
        return $this->hasMany(CityList::className(),['country_id'=>'id']);
    }

cityList.php

public static function tableName()
    {
        return 'city_list';
    }

Testcontroller控制器中

$model = Customer::find()->joinWith([
            'country'
        ])->asArray()->all();

猜你喜欢

转载自blog.csdn.net/weixin_43310339/article/details/86656713
今日推荐