【发布时间】:2016-06-16 15:29:32
【问题描述】:
查询执行得很好,但 Cakes 查询生成器没有将连接字段添加到 SELECT。我在这里想念什么? Cake 3.2.10、MySQL、Ubuntu。
$data = $this->Property->find()
->hydrate(false)
->join([
'PublisherProperty' => [
'table' => 'publisher_property', 'type' => 'inner',
'conditions' => "PublisherProperty.property_id = Property.id AND PublisherProperty.publisher_id = " . $this->Publisher->id
],
'PhysicalAddress' => [
'table' => 'property_address', 'type' => 'inner',
'conditions' => "PhysicalAddress.property_id = Property.id AND PhysicalAddress.type = 'physical'"
],
'CheckinAddress' => [
'table' => 'property_address', 'type' => 'left',
'conditions' => "CheckinAddress.property_id = Property.id AND CheckinAddress.type = 'checkin'"
],
'MainTelephone' => [
'table' => 'property_telephone', 'type' => 'inner',
'conditions' => "MainTelephone.property_id = Property.id AND MainTelephone.type = 'main'"
],
'ReservationTelephone' => [
'table' => 'property_telephone', 'type' => 'left',
'conditions' => "ReservationTelephone.property_id = Property.id AND ReservationTelephone.type = 'reservation'"
],
'PropertyDescription' => [
'table' => 'property_description', 'type' => 'left',
'conditions' => "PropertyDescription.property_id = Property.id AND PropertyDescription.publisher_id IN (" . implode(',',$publishers) . ")",
],
])
->where([
'Property.id' => 1111, //$request->property_id,
'Property.status' => 'ready',
])->first();
这是查询生成器最终执行的内容:
SELECT
Property.id AS `Property__id`,
Property.property_type_id AS `Property__property_type_id`,
Property.name AS `Property__name`,
Property.parent_company AS `Property__parent_company`,
Property.short_name AS `Property__short_name`,
Property.url AS `Property__url`,
Property.checkin_time AS `Property__checkin_time`,
Property.checkout_time AS `Property__checkout_time`,
Property.cutoff_days AS `Property__cutoff_days`,
Property.cutoff_time AS `Property__cutoff_time`,
Property.desk_open_time AS `Property__desk_open_time`,
Property.desk_close_time AS `Property__desk_close_time`,
Property.checkin_policy AS `Property__checkin_policy`,
Property.room_tax AS `Property__room_tax`,
Property.commission_rate AS `Property__commission_rate`,
Property.status AS `Property__status`,
Property.tripadvisor_location_id AS `Property__tripadvisor_location_id`,
Property.created AS `Property__created`,
Property.modified AS `Property__modified`
FROM
property Property
inner JOIN publisher_property PublisherProperty ON PublisherProperty.property_id = Property.id
AND PublisherProperty.publisher_id = 2
inner JOIN property_address PhysicalAddress ON PhysicalAddress.property_id = Property.id
AND PhysicalAddress.type = 'physical'
left JOIN property_address CheckinAddress ON CheckinAddress.property_id = Property.id
AND CheckinAddress.type = 'checkin'
inner JOIN property_telephone MainTelephone ON MainTelephone.property_id = Property.id
AND MainTelephone.type = 'main'
left JOIN property_telephone ReservationTelephone ON ReservationTelephone.property_id = Property.id
AND ReservationTelephone.type = 'reservation'
left JOIN property_description PropertyDescription ON PropertyDescription.property_id = Property.id
AND PropertyDescription.publisher_id IN (2, NULL)
WHERE
(
Property.id = 1111
AND Property.status = 'ready'
)
LIMIT
1
编辑:为了避免任何“你为什么这样做”的东西。我正在重写一个遗留应用程序,其中数据库命名约定与蛋糕命名约定不完全吻合,并且关系有点复杂。如果 contains 可以有效地查询数据库,我会使用 ORM,而不是。
【问题讨论】:
标签: cakephp orm cakephp-3.0