problem 'with' in ar

I have two tables and two models,there are Employee and GroupEmployee

id is employee’s pk,employee_id and group_id are groupemployee’s pk

in model Employee




public function getGroupEmployees($groupID)

    {

    	$criteria=new CDbCriteria;

    	$criteria->with=array(

		    'groupemployee',

		);

    	self::model()->findAll($criteria);

    }



but it’s error:

Relational "groupemployee" is not difined in active record class "Employee" .

i don’t know what’s wrong.

please help!

thanks!

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 - 

 self::model()->with('groupemployee')->findAll($criteria);

thank you for your reply!

i want use this way,




self::model()->with('groupemployee')->findAll();



but still have error like this:

Relational "groupemployee" is not difined in active record class "Employee" .

D:\home\yii\framework\db\ar\CActiveFinder.php(236)

Did you define relation groupemployee in your model class, as I showed above?

no,i didn’t,but i tried to defind,like this:




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.

do you see me?

i have no idea.

thank you very very much!

i find what’s wrong,i did not declare a relations in model Employee,it’s should be like this:




return array(

	'groups' => array(self::MANY_MANY, 'Group', 'groupemployee(group_id, employee_id)'),

);



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.

thank you both!

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.

doodle