I have a join table for a many to many relationship and I need to add a condition to the join table so only specific relationship types are returned. This is the relationship method in my User model…
public function getPayors()
{
return $this->hasMany(User::className(), ['id' => 'user_id'])
->viaTable('user_relationship', ['relation_user_id' => 'id'])
->onCondition(['relation_type_id' => UserRelationship::$USERRELATIONSHIP_TYPE['payor']]);
}
The problem with this is the condition is made on the user table not the user_relationship table so I get an error saying…
Column not found: 1054 Unknown column ‘relation_type_id’ in ‘where clause’
The SQL being executed was: SELECT * FROM user
WHERE (id
IN (‘4’, ‘67’)) AND (relation_type_id
=5)
I need to specify the condition on the user_relationship table. Is there any way I can achieve this? From my experience Yii2 does not handle join tables with additional columns other than the foreign keys very well.