Relation - Multiple Foreign Key 'or' Condition

Is it possible to have ONE relation which can match on two foreign keys using an OR condition?

For example I have a table called "messages" with two foreign keys - "from_user_id" and "to_user_id". I want to retrieve all messages "from" and "to" current user.

User model:


'messages'=>array(self::HAS_MANY, 'MessageThread', 'from_user_id, to_user_id'),

This creates an AND condition on the foreign key relation. How can I make it perform OR condition?

hi

Try It Now




'messages'=>array(self::HAS_MANY, 'MessageThread', 'from_user_id')

'messages2'=>array(self::HAS_MANY, 'MessageThread', 'to_user_id')



and




$criteria->condition = 'from_user_id = :temp OR to_user_id = :temp ';



As suggested above, you should probably have two different relations, perhaps called messagesSent and messagesReceived.

No worries. I was hoping it could all be done in one relation.

In the end I’ve just created a new function that does a normal CDbCriteria query to retrieve both sent and received.