Sto usando cakePHP 2.6.4.
Ho due tavoli AgentChat
e AgentChatMember
.
AgentChat
ha molti AgentChatMember
,
AgentChatMember
appartiene a AgentChat
.
AgentChat
avere: id, team_id, agent_chat_member_count
campi.
AgentChatMember
avere: id, agent_chat_id, user_id
campi.
voglio trovare AgentChat
di chat_team_id
e agent_chat_member_count
,
e AgentChatMember
come contengono con user_id
condizioni.
$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);
e $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"
)
)
Da questo ottengo quello che voglio, tranne il fatto che non posso impostare user_id
condizione a AgentChatMember
come questo: $options["conditions"]["AgentChatMember.user_id"] = $user_ids;
Quando sto usando join invece di contenere posso impostare le condizioni user_id, ma ottengo risultati come questo:
(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"
)
),
...
È possibile ottenere più record per unire in una tabella o contenere condizioni su di esso?
Come posso ottenerlo?
risposte:
0 per risposta № 1Puoi passare condizioni per il contenimento: -
$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
)
)
);
Per inciso, dovresti essere davvero un cammello che intelaia le tue variabili in CakePHP. Vedere il Standard di codifica nella documentazione.