2016-06-16 49 views
0

查詢執行得很好,但Cakes查詢生成器不會將連接字段添加到SELECT。我在這裏錯過了什麼? Cake 3.2.10,MySQL,Ubuntu。與CakePHP 3聯接查詢生成器不返回數據

 $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 

編輯:爲了避免任何「你爲什麼這樣做」的東西。我正在重寫一個傳統的應用程序,其中數據庫命名約定不適合與蛋糕命名約定,關係有點複雜。如果包含有效地查詢數據庫,我會使用ORM,而不是。

回答

0

想象一下,進入表模型並添加別名關係,因此您不必自定義寫查詢並可以使用contains。舉例PhysicalAddress以上進入PropertyTable並添加以下

$this->hasOne('PhysicalAddress', [ 
     'className' => 'PropertyAddress', 
     'foreignKey' => 'property_id', 
     'conditions' => ['PhysicalAddress.type'=>'physical'] 
    ]); 

然後在你找到剛纔確實包含(「PhysicalAddress」)