Answer the question
In order to leave comments, you need to log in
How to select only required columns in YII2 when eager loading?
The documentation says that you can manage a query when eager loading with an anonymous function:
When eagerly loading a relation, you can customize...
And an example is given:
$customers = Customer::find()->with([
'country',
'orders' => function ($query) {
$query->andWhere(['status' => Order::STATUS_ACTIVE]);
},
])->all();
...
'orders' => function ($query) {
$query->addSelect(['id', 'title', 'date']);
$query->andWhere(['status' => Order::STATUS_ACTIVE]);
},
...
Answer the question
In order to leave comments, you need to log in
Here is my request:
$data = AddressClassificator::find()
->select('id, iata')
->with(['addressClassificatorTranslates' => function($query) {
$query->addSelect(['id', 'name', 'country', 'city']);
$query->andWhere(['lang' => Yii::$app->language]);
}])
->asArray()
->all();
[
0 => [
'id' => '1'
'iata' => 'AAA'
'addressClassificatorTranslates' => []
]
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question