Answer the question
In order to leave comments, you need to log in
How to sort items by related table field in Yii2?
There are `ProductNomeclature` and `Brand` models. And the `Brand` model has `name` properties.
How to get the sorted rows of the ProductNomeclature table, by the `name` field of the `Brand` model?
The ProductNomeclature table has a "brand_code" field - this is the brand ID of the brand table.
After sampling, of course, I can sort the array, but I need sorting at the sampling stage (SQL query). Sorting an array AFTER the selection, by nested values does not suit me !!
In a query of this kind, the data that is selected from the associated brand table is sorted:
$items = ProductNomeclature::find()
->with([
'brand' => function ($query) {
$query->orderBy('name', 'ASC');
}
])
->all();
$items = ProductNomeclature::find()
->with(['brand'])
->orderBy('name', 'ASC')
->all();
Answer the question
In order to leave comments, you need to log in
with uses a separate query with IN(), the first query does not know anything about the second table, so you need to remake it for join queries
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question