In your model class there should be a method call relations and it should be like -
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'location'=>array(self::BELONGS_TO, 'location', 'LocationId'),
);
}
Here is the sample code to give you an example. Here my current model has LocationId as a foreign key which is pointing to my location table. I didn't understand your relations between the tables, so I couldn't write specific to your need. You need to read about - how to define relations in AR.
While calling your way is correct, other way is -
public function relations()
{
return array(
'employeeGroups' => array(self::HAS_MANY, 'groupemployee', 'employee_id'),
);
}
and
public function getEmployeesNotInStruct($groupID)
{
/*$criteria=new CDbCriteria;
$criteria->with=array(
//'groupstruct',
'groupemployee',
);
$criteria->joinType='INNER JOIN';
self::model()->findAll($criteria);*/
self::model()->with('groupemployee')->findAll();
}
still error
my db table struct
employee
id,name
groupemployee
group_id,employee_id
groupstruct
group_id,employee_id
i want find employees in the group and those employees not in the struct.
i write sql like this:
SELECT a. *
FROM employee AS a
LEFT JOIN groupemployee AS b ON b.employee_id = a.id
AND b.group_id =1
WHERE a.id NOT
IN (
SELECT employee_id
FROM groupstruct
WHERE group_id =1
)
but i don’t know how to implement in the model with AR.
and in the function getEmployeesNotInStruct,it’s should be:
public function getEmployeesNotInStruct($groupID)
{
return Employee::model()->with(array(
'groups'=>array('condition'=>'group_id='.$groupID),
))->findAll('t.id NOT IN (SELECT employee_id FROM groupstruct WHERE group_id = '.$groupID.')'
);
}
the results is what i want.
Thank you ps_sach,you give me some idea,and thank you Horacio Segura,he give me help too in another post.
Just a comment, if you use InnoDB engine (I am assuming mysql database) and you define your relations in the creation of the DB, the yiic tool will create these relations for you.
The only relations that I have had to add are MANY_MANY relations.