如何将多个记录合并为一个 table 或使用 contain with 条件?

How to get multiple records join into one table or use contain with conditions?

我正在使用 cakePHP 2.6.4。
我有两个 tables AgentChatAgentChatMember.
AgentChat 有许多 AgentChatMember,
AgentChatMember 属于 AgentChat.

AgentChat 有:id, team_id, agent_chat_member_count 个字段。
AgentChatMember 有:id, agent_chat_id, user_id 个字段。

我想通过 chat_team_idagent_chat_member_count,
找到 AgentChat AgentChatMember 包含 user_id 条件。

    $options['contain'] = array('AgentChatMember');
    $options['conditions']['AgentChat.agent_chat_member_count'] = count($user_ids);
    $options['conditions']['AgentChat.chat_team_id'] = $chat_team_id;

    $res = $this->AgentChat->find('first', $options);

$res是:

     'AgentChat' => array(
        'id' => '1',
        'agent_message_count' => '0',
        'agent_chat_member_count' => '2',
        'chat_team_id' => '14',
        'created' => '2015-05-06 09:52:31',
        'updated' => '2015-05-06 09:52:31'
    ),
    'AgentChatMember' => array(
        (int) 0 => array(
            'id' => '1',
            'agent_chat_id' => '1',
            'user_id' => '26',
            'created' => '2015-05-06 09:52:31'
        ),
        (int) 1 => array(
            'id' => '2',
            'agent_chat_id' => '1',
            'user_id' => '21',
            'created' => '2015-05-06 09:52:31'
        )
    )

通过这个我得到了我想要的,除了我不能像这样将 user_id 条件设置为 AgentChatMember$options['conditions']['AgentChatMember.user_id'] = $user_ids;

当我使用联接而不是包含时,我可以设置 user_id 条件,但我得到的结果是这样的:

(int) 0 => array(
        'AgentChat' => array(
            'id' => '1',
            'agent_message_count' => '0',
            'agent_chat_member_count' => '2',
            'chat_team_id' => '14',
            'created' => '2015-05-06 09:52:31',
            'updated' => '2015-05-06 09:52:31'
        ),
        'AgentChatMember' => array(
            'id' => '2',
            'agent_chat_id' => '1',
            'user_id' => '21',
            'created' => '2015-05-06 09:52:31'
        )
    ),
    (int) 1 => array(
        'AgentChat' => array(
            'id' => '1',
            'agent_message_count' => '0',
            'agent_chat_member_count' => '2',
            'chat_team_id' => '14',
            'created' => '2015-05-06 09:52:31',
            'updated' => '2015-05-06 09:52:31'
        ),
        'AgentChatMember' => array(
            'id' => '2',
            'agent_chat_id' => '1',
            'user_id' => '26',
            'created' => '2015-05-06 09:52:31'
        )
    ),
    ...

是否可以将多个记录合并为一个 table 或包含条件?
我怎样才能做到这一点?

你可以通过conditions to the contain:-

$res = $this->AgentChat->find(
    'first',
    array(
        'contain' => array(
            'AgentChatMember' => array(
                'conditions' => array(
                    'AgentChatMember.user_id' => $userIds
                )
            )
        ),
        'conditions' => array(
            'AgentChat.agent_chat_member_count' => count($userIds),
            'AgentChat.chat_team_id' => $chatTeamId
        )
    )
);

顺便说一句,您真的应该在 CakePHP 中对变量进行驼峰式封装。请参阅文档中的 Coding Standards