0

I have following model associations:

//User Model
public $hasMany = array(
    'MessageSent' => array(
        'className' => 'Message',
        'foreignKey' => 'sender_id'
    ),
    'MessageReceived' => array(
        'className' => 'Message',
        'foreignKey' => 'receiver_id'
    ));

//Message Model
public $belongsTo = array(
    'Sender' => array(
        'className' => 'User',
        'foreignKey' => 'sender_id'
    ),
    'Receiver' => array(
        'className' => 'User',
        'foreignKey' => 'receiver_id'
    )
);

Now i would like to fetch a User with the messages ordered by "created", so that i can create a message stream ala facebook.

$this->User->contain(array(
    'MessageSent' => array('conditions' => array('MessageSent.receiver_id' => 
        $this->Auth->user('id'))),
    'MessageReceived' => array('conditions' => array('MessageReceived.sender_id' =>
        $this->Auth->user('id')))
    ));
$partner = $this->User->find('all', array('conditions' => array('slug' =>
    $this->request->params['slug'])));

So i need something like "ORDER BY Message.created" so that i get MessageSent and MessageReceived mixed together and ordered by date . How do i do that?

4

1 回答 1

0

您可以只从 Message 模型而不是 User 模型进行查询吗?

例如:

$userId = $this->Auth->user('id');
$this->Message->find('all',
    array(
        'conditions' => array(
            'OR' => array(
                'Message.sender_id' => $userId,
                'Message.receiver_id' => $userId
            )
        ),
        'contain' => array('Sender', 'Receiver'),
        'order' => 'Message.created DESC'
    )
);
于 2013-05-28T11:37:58.317 回答