【问题标题】:Joins with CakePHP 3 Query Builder Not Returning Data加入 CakePHP 3 查询生成器不返回数据
【发布时间】: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


    【解决方案1】:

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

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

    然后在你的查找中包含('PhysicalAddress')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 2016-12-31
      • 2020-06-24
      相关资源
      最近更新 更多