Additional Criteria For Relations

Hello,

I have a nested related database and I’m retrieving the data as follow:


$Criteria = new CDbCriteria();

$Criteria->compare('company_id',$userObject->profile->company_id);

$Criteria->with=array('employee'=>array('select'=>'name'), 'days'=>array('with'=>array('entries'=>array('with'=>array('job','overhead')))));

$this->_model=TimesheetEmployee::model()->findAll($Criteria);

Above returns all matching related ‘entries’ as expected however I’m wondering how I can add another condition for ‘entries’ so it also matches the column ‘day’ of its related table with value of ‘day’ of it’s own column?

Both tables have ‘day’ column


table-1

id:1, day:mon


table-2

id:1, related_id:1, day:mon

id:2, related_id:1, day:tue

Also, the line below doesn’t seem to be working, instead of only selecting and returning ‘name’ column it returns all columns


'employee'=>array('select'=>'name')

Use relation names as table aliases in query conditions, like "entries.day".

As for the select, it seems it doesn’t work in ‘with’ param. Try setting it in the main criteria object like “t.*, employee.name”. That will select all columns from the main table.

Thank you, I think adding following line should do the trick then (haven’t tested yet)

$Criteria->condition = ‘entries.day=days.day’;

With the select I think I followed a tutorial somewhere it was an official Yii page though (might have been user submitted example no sure), will try what you have suggested