首先建立是三张表
国家表 城市表 用户表
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();