Hello,
I’ve got Member(AR class) which has id(primary key) and other properties, and corresponds to database table member
. I have another table invitation
for relationship between members to represent “member a invites member b”. Thus, it has two fields from
and to
.
My goal is to achieve this query using AR in order to find out whom I invited before:
SELECT username FROM member m INNER JOIN invitation i ON i.to = m.id AND i.from = $_GET['id'];
I filled out the relations() method in Member AR class:
'invitation'=>array(self::MANY_MANY, 'Member', 'invitation(from, to)'),
I also tried to create CActiveDataProvider as follows:
$invitationDataProvider=new CActiveDataProvider('Member', array(
'criteria'=>array(
'with'=>array(
'invitation'=>array(
'on'=>'invitation.to = t.id AND invitation.from = '.$_GET['id'],
'joinType'=>'INNER JOIN',
)
),
'order'=>'time DESC',
),
));
It throws the exceptions:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invitation.to' in 'on clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `member` `t` INNER JOIN `invitation` `invitation_invitation` ON (`t`.`id`=`invitation_invitation`.`from`) INNER JOIN `member` `invitation` ON (`invitation`.`id`=`invitation_invitation`.`to`) AND (invitation.to = t.id AND invitation.from = 74)
It seems that I did not specify both relationship and criteria because I don’t understand the relationship correctly. Please, correct me. Thank you.