I have a class Group
, which hasMany Teams
and I would like to order them by their score. A team's score is calculated by iterating through its games, so score is not a db-field, it's a virtual read-only attribute.
This is my attempt:
public function getTeams()
{
return $this->hasMany(Team::className(), ['group' => 'id'])->orderBy(['score' => SORT_ASC]);
}
This does not work, as score is not a db-field. How can I order a relation by a virtual attribute?