Hi,
I have the following problem:
I have a MANY_MANY connection for the tables tbl_ddentity and tbl_deviceclass (mapped via tbl_ddentityxdeviceclass).
Now I added the following relation to DDEntity.php controller:
public function relations()
{
return array(
'deviceClasses'=>array(self::MANY_MANY, 'DeviceClass',
'ddentityxdeviceclass(sysDDEntityID, sysDeviceClassID)'),
);
}
and this one to DeviceClass.php controller:
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(
'dDEntitys'=>array(self::MANY_MANY, 'DDEntity',
'ddentityxdeviceclass(sysDeviceClassID, sysDDEntityID)'),
);
}
If I access the model via ActiveDataProvider, I can easily access DeviceClasses for a DDEntity via:
foreach ($model->DeviceClasses as $deviceClass)
print_r ($deviceClass);
But:
If I try to get only DDEntities which have certain DeviceClasses via the model search function, it seems as if the relations are not already known to Yii:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('deviceClasses.DeviceClassNumber',$this->DeviceClassNumber);
return new CActiveDataProvider('DDentity', array(
'criteria'=>$criteria,
'sort'=>$sort,
'pagination'=>array(
'pageSize'=>20,
),
));
}
I added "DeviceClassNumber" as a public attribute to DDEntity and now get the SQL error
MYSQL Error: Unknown Column in DeviceClasses.DeviceClassNumber
Now my question:
I could of course generate the needed SQL statement by hand (table joins and where clause).
But as I have already declared the relations, it would be much easier, if I could access them here.
What is the best (easiest and fastest) way to do this?
Thanks a lot,
Simon
P.S.:
I stripped loads of code to make it easier to read. It might be that there is some minor error which prevents this particular code from working. But I hope my general problem is obvious.