如何将多个记录连接到一个表或使用包含条件?

时间:2021-12-01 23:06:07

I'm using cakePHP 2.6.4.
I have two tables AgentChat and AgentChatMember.
AgentChat hasMany AgentChatMember,
AgentChatMember belongsTo AgentChat.

我正在使用cakePHP 2.6.4。我有两个表AgentChat和AgentChatMember。 AgentChat有很多AgentChatMember,AgentChatMember属于AgentChat。

AgentChat have: id, team_id, agent_chat_member_count fields.
AgentChatMember have: id, agent_chat_id, user_id fields.

AgentChat包含:id,team_id,agent_chat_member_count字段。 AgentChatMember具有:id,agent_chat_id,user_id字段。

I want to find AgentChat by chat_team_id and agent_chat_member_count,
and AgentChatMember as contain with user_id conditions.

我想通过chat_team_id和agent_chat_member_count找到AgentChat,并通过user_id条件找到AgentChatMember。

    $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);

and $res is:

和$ 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'
        )
    )

By this i get what i want except fact i cant set user_id condition to AgentChatMember like this: $options['conditions']['AgentChatMember.user_id'] = $user_ids;

通过这个我得到我想要的,除了事实我不能将user_id条件设置为AgentChatMember,如下所示:$ options ['conditions'] ['AgentChatMember.user_id'] = $ user_ids;

When i'm using joins instead of contain i can set user_id conditions, but i get result like this:

当我使用连接而不是包含我可以设置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'
        )
    ),
    ...

Its possible to get multiple records join into one table or contain with conditions on it?
How can i achieve that?

可以将多个记录连接到一个表中或包含条件吗?我怎样才能实现这一目标?

1 个解决方案

#1


You can pass 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
        )
    )
);

Just as an aside, you should really be camel casing your variables in CakePHP. See the Coding Standards in the documentation.

顺便说一下,你应该在CakePHP中使用camel封装你的变量。请参阅文档中的编码标准。

#1


You can pass 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
        )
    )
);

Just as an aside, you should really be camel casing your variables in CakePHP. See the Coding Standards in the documentation.

顺便说一下,你应该在CakePHP中使用camel封装你的变量。请参阅文档中的编码标准。