如何在cakephp 3.x中使用两个左连接

时间:2017-07-28 05:21:56

标签: php mysql cakephp cakephp-3.x

$table = TableRegistry::get('Leads');
$query = $table->find('all')->leftJoinWith('LeadStatus')->contain(['Users', 'LeadStatus' =>
function ($q)
    {
    return $q->contain(['LeadBuckets', 'LeadBucketSubStatus'])->where(['LeadStatus.is_active' => 1]);
    }

])->where(['Leads.sub_agent_id' => $subAgentId]);

这是我的查询,我在表leads中使用左连接表lead_status。现在,我还想在同一个查询中使用左连接作为第三个表assigned_leads

我尝试将已加入的表leadslead_statusassigned_leads联系起来。我正在使用cakephp 3.x我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我已经使用左连接解决了这个问题,如下所示:

$query = $table->find('all')->leftJoinWith('LeadStatus')
          ->leftJoinWith('AssignedLeads')
          ->contain(['AssignedLeads'=>'Users','LeadStatus' => function($q)
               {
                  return $q->contain(['LeadBuckets', 'LeadBucketSubStatus'])
                     ->where(['LeadStatus.is_active' => 1]);
               }
               ])
          ->where(['Leads.sub_agent_id' => $subAgentId])
          ->where(['AssignedLeads.user_id IN' => $userId]);