Use Multiple Joins in CakePHP database calls

Use Multiple Joins in CakePHP database calls

This example shows you how you can get related user information for the created_by and modified_by users, separately.

In your Controller:


$joins = [
            'created_by' => [
                'table' => 'users',
                'type' => 'LEFT',
                'conditions' => 'created_by.id = SupportTickets.created_by',
            ],
            'assigned_to' => [
                'table' => 'users',
                'type' => 'LEFT',
                'conditions' => 'assigned_to.id = SupportTickets.assigned_to',
            ],
            'modified_by' => [
                'table' => 'users',
                'type' => 'LEFT',
                'conditions' => 'modified_by.id = SupportTickets.modified_by',
            ]
            ];
        $fields = ['SupportTickets.id', 'SupportTickets.user_id', 'SupportTickets.assigned_to', 'SupportTickets.subject', 'SupportTickets.description', 
        'SupportTickets.priority', 'SupportTickets.status', 'SupportTickets.uri', 'SupportTickets.created', 'SupportTickets.modified', 
        'SupportTickets.created_by', 'SupportTickets.modified_by',
        'created_by.username', 'assigned_to.username', 'modified_by.username'];

 

Share this Post