condition while making relation in YIi
12,050
Solution 1
add the condition on your relation
public function relations() {
return array(
'user' => array(self::HAS_ONE, 'Users', 'f_id', array(
'condition' => 'user.type = :type',
'params' => array(':type'=>3)
)),
);
}
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-options
Solution 2
There is no error like 'Property "CHasOneRelation.0" is not defined' if you use this:
public function relations()
{
return array(
'user' => array(
self::HAS_ONE,
'Users',
'f_id',
'on' => 'user.ref_type = :type',
'params' => array(':type' => 3))
);
}
See this link: http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/
Author by
abnab
Updated on June 28, 2022Comments
-
abnab almost 2 years
Agent:
agent_id (primary key)
User:
f_id (foreign key) type
I have created relation in this way
public function relations() { return array( 'user' => array(self::HAS_ONE, 'Users', 'f_id'), ); }
But I want to add more conditions like join only if
type=3
in User table.thanks.