CakePHP - 包含在find中的不需要的HABTM使用“包含”

时间:2012-01-23 21:07:10

标签: cakephp

我正在使用CakePHP创建一个消息传递系统。在模型方面,

Users HABTM Conversations
Conversations hasMany Messages
Messages belongTo User (the sender)

所以我在用户上运行查找条件,包含如下:

$user = $this->Conversation->User->find('first', array(
        'conditions' => array('User.id' => $user_id),
        'contain' => array(
            'Conversation' => array(
                'ConversationsUser',
                'Message' => array(
                    'order' => 'Message.created DESC',
                    'limit' => 1,
                    'User' => array(
                        'Student' => array(
                            'University'
                        ),
                        'Recruiter' => array(
                            'Org'
                        )
                    )
                )
            )
        )
    ));

当我得到结果时,我得到$ user ['Conversation'] ['Message'] ['User'] ['Conversation'] - 基本上,HABTM连接会自动完成3级深度而且没有办法我要防止它。我有什么方法可以从查找中删除此信息吗?

1 个答案:

答案 0 :(得分:0)

解决。我通过以下方式对结果进行了重组:

$this->Conversation->bindModel(array('hasOne' => array('ConversationsUser')));
$conversations = $this->Conversation->find('all', array(
        'conditions' => array('ConversationsUser.user_id' => $user_id),
        'order' => 'Conversation.modified DESC',
        'contain' => array(
            'ConversationsUser',
            'User' => array(
                'conditions' => array('User.id <> ' . $user_id),
                'ProfilePhoto' => array(
                    'Attachment'
                ),
                'Student' => array(
                    'University'
                ),
                'Recruiter' => array(
                    'Org'
                )
            ),
            'Message' => array(
                'order' => 'Message.created DESC'
            )
        )
    ));

我真的不需要在消息中拥有用户。