Case 1:
I have this entities:
User
- idUser
Message
-
idUserFrom
-
idUserTo
So, I want to declare a HAS_MANY relation in the User model, which has all the messages that the user received OR sent, but I don’t know how to specify two different foreign keys ( idUserFrom & idUserTo ) and a condition that states that the relation should join by idUserFrom, when idUserFrom its equal to isUser, and by idUserTo, when idUserTo is equal to idUser.
Case 2:
It’s a similar case, but with a MANY_MANY relation, with the following table:
Friendship
-
idUser1
-
idUser2
The friendship relationship is bi-directional, meaning that (1,2) and (2,1) represent the same friendship relationship between users 1 and 2, and if one combination exists in the table, the other doesn’t, each friendship is represented by a single row in the friendship table.
To define a relation in the model, I would need to specify that the relation should join a User 1 with another User 2, where exists a Friendship that has ( user 1, user 2) OR ( user 2, user 1 )
The Yii documentation doesn’t explain this very well, and when it shows an example of using a ‘condition’ inside a relation, it uses a trivial example which doesn’t include foreign keys, such as ‘status=1’