January 19, 2009, 11:26am
i have the folloing relation: user <- user_relation -> user
user_relation is a n:m table with id as pk, fromid and toid. Is it possible to query the relations in this way?
The problem is that my user_relation has a PK called "id" and this will be joined with the user table and not my toid column property.
Is there a way to handle this?
January 19, 2009, 12:42pm
How did you declare the relation rule in the user AR?
The query will not bring in 'id' column from user_relation.
January 19, 2009, 7:29pm
in the user class i have the following rule
'relations' => array(self::MANY_MANY, 'User', 'user_relation(fromid, toid)'),
and in the user_relation
'user' => array(self::HAS_ONE, 'User', 'id')
January 19, 2009, 7:41pm
so what is your problem??
January 19, 2009, 8:00pm
this is my code:
$criteria = new CDbCriteria;
$criteria->limit = $limit;
$criteria->offset = $offset;
$relations = Relation::model()->with('friend')->findAllByAttributes(array('fromid'=>$id), $criteria);
and the call generates this:
relationid AS t0_c0, …
LEFT OUTER JOIN
user t1 ON t1.
fromid='1' LIMIT 4
these are my models:
the question is, why does it take fromid? Does it take automaticly the first column? Cant i define which local column in the table is responsible for the join?
I mean in the relation rule like:
'user' => array(self::HAS_ONE, 'User', 'remote'=>'id', 'local'=>'toid')
January 19, 2009, 8:19pm
It is based on FK constraint which defines a pair. It is not arbitrary. I would suggest you change your table schema because it is not well designed (mainly the FK constraints).
January 19, 2009, 8:50pm
i cant follow…
i have the relation user -> user_relation -> user
so user_relation has to FKs back to the user table (fromid, toid). How can the code know which FK to take if i do user_relation::model()->find(…)
I dont think, that this might be a wrong approach for a n:m. Can you give more hints?
January 19, 2009, 8:58pm
When you declare a relation rules in relations(), you only need to specify the FK, not PK. Since a FK is always associated with a PK, that's why Yii knows which PK is relating with.
January 19, 2009, 9:03pm
What would be your solution in this case?
I want to define two relations in the user_relation, 1. fromuser, 2.touser.
I have defined two FKs in the database, but how can i define in the relations() that for the fromuser relation the column/FK fromid is responsible and for the touser the column/FK toid.
Thanks for your really quick replies…
January 19, 2009, 9:15pm
I don't know why you want to define a user_relation model. The user_relation table is mainly used to decouple n:m self-relationship in user table. So all you need is just the user model. Then, if you want to find out a user's friends, you can declare in user model a relation like the following:
January 19, 2009, 9:20pm
Ok, now its getting hotter
I have actually this relation you mentioned. I have had a thread where i asked you if i can access the user_relation data if i would use user::model()->with('friends')…
You told me that the n:m table cant have any data, but what i need
So we agreed to try to query the user_relation directly…
January 19, 2009, 9:24pm
Yes, that's the limit of RAR because it can't model the relation table very well.