我正在使用cakePHP 2.6.4
我有两张表AgentChat
和AgentChatMember
AgentChat
有许多AgentChatMember
,
AgentChatMember
属于AgentChat
。
AgentChat
有:id, team_id, agent_chat_member_count
个字段
AgentChatMember
有id, agent_chat_id, user_id
个字段。
我希望AgentChat
和chat_team_id
找到agent_chat_member_count
,
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'
)
),
...
可以将多个记录连接到一个表中或包含条件吗?
我怎样才能做到这一点?
答案 0 :(得分:0)