I have a many to many relationship in FuelPHP as seen below:
protected static $_many_many = array(
'members' => array(
'key_from' => 'team_id',
'key_through_from' => 'team_id',
'table_through' => 'user_has_team',
'key_through_to' => 'user_id',
'model_to' => 'Model_User',
'key_to' => 'id',
)
);
But I wanted to know if you can have a where clause in the relationship. For example:
protected static $_many_many = array(
'members' => array(
'key_from' => 'team_id',
'key_through_from' => 'team_id',
'table_through' => 'user_has_team',
'key_through_to' => 'user_id',
'model_to' => 'Model_User',
'key_to' => 'id',
'where' => array('account_status' => 'active')
)
);
So it only returns Model_User objects which have their account_status set to ‘active’. I know this is pushing it a bit but Fuel is awesome in lots of other was so I though there might be a way to do this.
Obviously you could do this with a query but I wanted to know if there is a way to do it using $_many_many
Bit late to the party, but for people hitting this one on a search:
You can define conditions on a relationship definition. At the moment ‘where’ and ‘order_by’ clauses are supported.
This conditions are permanent, you can’t switch them on or off. So if you need both full and filtered access to a related model, you will have to define two relations.
See http://fuelphp.com/docs/packages/orm/relations/intro.html#usage_rel_conditions for more information.