【问题标题】:How to get multiple records join into one table or use contain with conditions?如何将多条记录加入一个表或使用包含条件?
【发布时间】:2015-05-06 08:30:05
【问题描述】:

我正在使用 cakePHP 2.6.4。
我有两张表AgentChatAgentChatMember
AgentChat hasMany 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'
        )
    ),
    ...

是否可以将多条记录连接到一个表中或包含在其上的条件?
我怎样才能做到这一点?

【问题讨论】:

    标签: mysql cakephp join


    【解决方案1】:

    你可以通过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

    【讨论】:

    • 我实际上尝试过这种方式,所以我弄乱了语法。无论如何感谢您的帮助和建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多