Bhinal Chauhan
Bhinal Chauhan

Reputation: 251

How to user two left joins in 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]);

This is my query where I am using left join for table leads from table lead_status. Now, I also want to use left join for third table assigned_leads in same query.

I have tried association of joined tables leads and lead_status with assigned_leads. I am using cakephp 3.x How can I achieve this?

Upvotes: 1

Views: 516

Answers (1)

Bhinal Chauhan
Bhinal Chauhan

Reputation: 251

I have solved this using left join as shown below:

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

Upvotes: 1

Related Questions