如何在cake 3.x中强制自定义查询的类型?

时间:2016-03-20 10:49:38

标签: cakephp cakephp-3.0

我现在有什么:

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

我得到了什么

name: "Song 123",
email: "abc@abc.com",
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"

我的期望

我希望event_package_id为整数

我尝试了什么

我在查询后添加了这个。

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

我得到了什么错误?

 Undefined index: EventTicketSalesDelegates.event_package_id 

1 个答案:

答案 0 :(得分:0)

除非您在select()调用中定义自定义别名,否则在添加类型时必须使用CakePHP默认别名格式,即

AssociationName__column_name

所以在你的情况下EventTicketSalesDelegates__event_package_id

您还可以通过Query::aliasField()以编程方式检索别名,例如

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