Kim Stacks
Kim Stacks

Reputation: 10812

How to force the type for a custom query in cake 3.x?

What I currently have:

 $paginationQuery
                ->select([
                    'EventTicketSalesDelegates.attending', 
                    'EventTicketSalesDelegates.event_package_id',
                    'EventPackages.name', 'EventPackages.id',
                    'EventPackages.description',
                    'Contacts.id', 'Contacts.name', 'Contacts.email',
                    'Contacts.mobile', 'Contacts.company',
                    'Contacts.phone', 'Contacts.fax', 'Contacts.address_one', 
                    'Contacts.address_two', 'Contacts.comments', 'Contacts.job_title', 
                ])
                ->join(
                    [
                        'EventTicketSalesDelegates' => [
                            'table'      => 'event_ticket_sales_delegates',
                            'type'       => 'INNER',
                            'conditions' => [
                                'EventTicketSalesDelegates.contact_id = Contacts.id'
                            ]
                        ],
                        'EventTicketSales' => [
                            'table'      => 'event_ticket_sales',
                            'type'       => 'INNER',
                            'conditions' => [
                                'EventTicketSalesDelegates.event_ticket_sale_id = EventTicketSales.id'
                            ]
                        ],
                        'EventPackages' => [
                            'table'      => 'event_packages',
                            'type'       => 'INNER',
                            'conditions' => [
                                'EventPackages.id = EventTicketSalesDelegates.event_package_id'
                            ]
                        ]
                    ]
                )->where(['EventTicketSales.id' => $eventTicketSaleId]);

What I get

name: "Song 123",
email: "[email protected]",
mobile: "+62 123",
company: "SK ",
phone: "+62 123",
fax: "",
address_one: "",
address_two: "",
comments: "",
job_title: "Director",
EventTicketSalesDelegates: {
attending: "1",
event_package_id: "53"
}
,
EventPackages: {
name: "Roundtable Forum Only",
id: "53",
description: "Roundtable Forum Only(3 Mar 2016 9:00 - 4 Mar 2016 12:00)"
},
attending: "1",
event_package_id: "53"

What I expected

I want the event_package_id to be integer

What I tried

I added this after the query.

  $paginationQuery
        ->selectTypeMap()
        ->addDefaults([
            'EventTicketSalesDelegates.event_package_id' => 'integer',
        ]);

What error I got?

 Undefined index: EventTicketSalesDelegates.event_package_id 

Upvotes: 0

Views: 61

Answers (1)

ndm
ndm

Reputation: 60463

Unless you are defining a custom alias in the select() call, you must use the CakePHP default aliasing format when adding a type, that is

AssociationName__column_name

So in your case EventTicketSalesDelegates__event_package_id.

You can also retrieve the alias programmatically via Query::aliasField(), like

key($paginationQuery->aliasField('event_package_id', 'EventTicketSalesDelegates'))

Upvotes: 0

Related Questions