与CakePHP 3查询生成器连接不返回数据

时间:2016-06-16 15:29:32

标签: cakephp orm cakephp-3.0

查询执行得很好,但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

编辑:为了避免任何"你为什么这样做?"东西。我正在重写一个遗留应用程序,其中数据库命名约定与蛋糕命名约定不完全吻合,并且关系有点复杂。如果包有效地查询数据库,我会使用ORM,不是。

1 个答案:

答案 0 :(得分:0)

想出这个,进入Table模型并添加别名关系,这样您就不必自定义编写查询并且可以使用contains。上面的PhysicalAddress示例进入PropertyTable并添加以下内容

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

然后在你的发现中只做包含('PhysicalAddress')