Answer the question
In order to leave comments, you need to log in
Is it possible to preserve the order specified in the intermediate table when selecting through relationships?
There are 2 tables - patterns and actions, interconnected through an intermediate table (many-to-many). I need to get all the actions of a pattern, in a specific order. I do it through connections.
In the pattern model, I did this:
public function getPatternMoves()
{
return $this->hasMany(PatternMove::className(), ['pattern_id' => 'id'])
->orderBy('order_num ASC');
}
public function getMoves()
{
return $this->hasMany(Move::className(), ['id' => 'move_id'])
->via('patternMoves');
}
SELECT * FROM `pattern_moves` WHERE `pattern_id`=1 ORDER BY `order_num`
SELECT * FROM `moves` WHERE `id` IN (4, 2, 3)
Answer the question
In order to leave comments, you need to log in
public function getMoves()
{
return $this->hasMany(Move::className(), ['id' => 'move_id'])
->viaTable('pattern_moves', ['pattern_id' => 'id'])->orderBy('order_num ASC');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question