LEFT OUTER JOIN user t1 ON t1.id=user_relation.fromid
WHERE user_relation.fromid='1' LIMIT 4
these are my models:
user:
id (pk)
firstname
lastname
…
user_relation:
fromid
toid
…
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?
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).
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.
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.
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:
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…