Hi there,
How can I parameterise relations function in CActiveRecord? Here’s the situation.
group Table (Database)
id
name
contact Table (Database)
id
name
mobile_no
group-contact Table (Database): links the two tables together.
group_id
contact_id
How is it possible to get contacts under a group by CActiveDataProvider? I’ve tried:
In ContactTbl CActiveRecord (contact table model):
public function relations() {
return array(
'group' => array(self::MANY_MANY, 'GroupTbl', 'group-contact(group_id, contact_id)',
'select' => 'group.id'
),
);
}
public function listingByGroup($group, $page = 0) {
$criteria = new CDbCriteria();
$criteria->select = array('t.id', 't.name', 't.mobile_no');
$criteria->condition = 't.deleted IS NULL AND group.id = :group';
$criteria->params = array(':group' => $group);
$criteria->order = 't.name, t.mobile_no';
$criteria->with = 'group';
return new CActiveDataProvider('ContactTbl', array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 1,
'currentPage' => $page,
),
));
}
Error:
[color="#FF0000"]
2010/05/06 07:43:13 [error] [system.db.CDbCommand] Error in querying SQL: SELECT t
.id
AS t0_c0
, t
.name
AS t0_c1
, t
.mobile_no
AS t0_c2
FROM contact
t
WHERE (t.deleted IS NULL AND group.id = :group) ORDER BY t.name, t.mobile_no LIMIT 1. Bind with parameter :group=1
2010/05/06 07:43:13 [error] [exception.CDbException] exception ‘CDbException’ with message ‘CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘group.id’ in ‘where clause’’ in \apache_server\yii1.1\framework\db\CDbCommand.php:375
[/color]