I have three database table:
product (id, name)
product_has_adv (product,advantage,sort,important)
advantage (id, text)
In ProductModel I defined this:
public function getAdvantages()
{
return $this->hasMany(AdvantageModel::className(), ['id' => 'advantage'])
->viaTable('product_has_advantage', ['product' => 'id']);
}
I get the advantages without any problems.
But now I need to add a where product_has_advantage.important = 1 clausel and also sort the advantages by the sort-columen in the product_has_advantage-table.
How and where I have to realize it?
First you need to create a model named
ProductHasAdv
for junction table (product_has_adv
) using CRUD.Then create relation in
product
model and sort it:Then create second relationship like this:
This will sort final result using
order by FIELD
technique.Don't forget to add:
line to head.
Using
via
andviaTable
methods with relations will cause two separate queries.You can specify callable in third parameter like this:
The filter by
important
will be applied, but the sort won't since it happens in first query. As a result the order of ids inIN
statement will be changed.Depending on your database logic maybe it's better to move
important
andsort
columns toadvantage
table.Then just add condition and sort to the existing method chain:
I`ve managed this some how... but it needs additional work after. The point is that you have to query many-to-many relation first from source model and after that inside that closure you should query your target model.
Then you just have to prettify the sorted result to your needs. The result for each row would look like
For who comes here after a while and don't like above solutions, I got it working by joining back to the via table after the filter via table.
Example for above code:
Take care about changing XXX with the right join path and YYY with the right sort column.
Using
viaTable
methods with relations will cause two separate queries, but if you don't needlink()
method you can use innerJoin in the following way to sort by product_has_advantage table:Note than
$query->multiple = true
allows you to use this method as Yii2 hasMany relation.Just for reference https://github.com/yiisoft/yii2/issues/10174 It's near impossible to
ORDER BY viaTable()
columns. For Yii 2.0.7 it returns set of ID's fromviaTable()
query, and final/top queryIN()
clause ignores the order.