I have two relations defined in my Model to the same table
public function getCountry(){
return $this->hasOne(Country::className(),['country_id' => 'country_id']);
}
public function getCurrency(){
return $this->hasOne(Country::className(), ['country_id' => 'currency']);
}
I want to join both relation in my query. Below code is showing error.
Country::find()->joinWith(['country','currency'])->....
Tried this too
Country::find()->joinWith(['country','currency as cur'])->....
How to specify alias for the second relation ??
Since Yii 2.0.7:
->joinWith(['country', 'currency cur'])... // Note we dont use `as`, just an space
Source: Yii2 Guide
You can give alias to particular relation as below :
->joinWith([
'country',
'currency' => function ($q) {
$q->from(Country::tableName() . ' cur');
}
])
Refer this thread to get further details - https://github.com/yiisoft/yii2/issues/2377#issuecomment-34573765
without relations
Category::find()
->select('c1.*')
->from('category c0')
->innerJoin('category as c1', 'c1.parent_id = c0.id')
->where(['c0.slug' => $parent]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With