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