0

I have a mail_conversation and a mail_conversation_member table.

mail_conversation has only id as the PRIMARY KEY.

mail_conversation_member has conversation_id and user_id as PRIMARY KEYs.

Now, I want to list a conversation and all its members, but it only returns one member. It seems I need to add a second PRIMARY KEY in the HAS_MANY relationship.

 'message_member' => array(self::HAS_MANY, 'MailConversationMember', 'conversation_id')

I get the conversations with:

 $criteria = new CDbCriteria();
        $criteria->condition = "message_member.user_id = " . Yii::app()->user->id;
        $criteria->order = 'messages.date_created DESC';

        $mail_conversations = MailConversation::model()->with(array('message_member',   'messages'))->findAll($criteria);
Expedito
  • 7,771
  • 5
  • 30
  • 43
Garry Cat
  • 31
  • 1
  • 8

1 Answers1

0

This is because of the criteria you have. If you take the 'message_member' out of the with array, you can lazy load the members once you have the conversations. You should check the SQL code that Yii is generating to confirm that the criteria is being interpreted as expected.

In addition, why not define a relationship in the member class:

'conversations' => array(self::HAS_MANY, 'MailConversationMember', 'user_id',array(
     'order' => 'messages.date_created DESC',
),

Then you can pull the conversations through without that criteria you have specified, and pull the members through on each conversation.

mwotton
  • 2,170
  • 18
  • 36