簡體   English   中英

格式化CakePHP連接語句

[英]Formatting CakePHP join statement

我正在使用CakePHP 2.3.8,並且試圖更有效地連接兩個表。 建築物和building_rental_rates

buildings
id  |   name  |   description    |  property_owner  |  building_type
 1      Big      Big Building               1               1

building_rental_rates
id  |   building_type |   rate_name    |      rate
 1            1              daily          150.00
 2            1              hourly         15.00

我想查找一棟建築物並選擇不同的租金。 這些表在building_type上聯接。 這是我的查找聲明

$buildings = $this->Building ->find('all',array(
                        'Building.property_owner' => '1',
                        'fields' => array('Building.*','BuildingRentalRate.*'),
                        'joins' => array(
                            array(
                                'table' => 'building_rental_rates',
                                'alias' => 'BuildingRentalRate',
                                'type' => 'inner',
                                'conditions' => array(
                                    'Building.building_type = BuildingRentalRate.building_type'
                                )
                            )
                        )
                    ));

這是結果

Array
(
    [0] => Array
        (
            [Building] => Array
                (
                    [id] => 1
                    [name] => Big
                    [description] =>  Big Building
                    [property_owner] => 1
                    [building_type] => 1
                )

            [BuildingRentalRate] => Array
                (
                    [id] => 1
                    [building_type] => 1
                    [rate_name] => daily
                    [rate] => 150.00
                )

        )

    [1] => Array
        (
            [Building] => Array
                (
                    [id] => 1
                    [name] => Big
                    [description] =>  Big Building
                    [property_owner] => 1
                    [building_type] => 1
                )

             [BuildingRentalRate] => Array
                (
                    [id] => 2
                    [building_type] => 1
                    [rate_name] => hourly
                    [rate] => 15.00
                )

        )
)

雖然正確找到了數據,但遍歷它很痛苦。 我可以使用join語句生成此輸出嗎? 請注意,BuildingRentalRate是一個數組,其中包含該表的所有條目,它們共享相同的building_type

Array
(
    [0] => Array
        (
            [Building] => Array
                (
                    [id] => 1
                    [name] => Big
                    [description] =>  Big Building
                    [property_owner] => 1
                    [building_type] => 1
                )

            [BuildingRentalRate] => Array
                (
                  [0] => Array
                     (
                        [id] => 1
                        [building_type] => 1
                        [rate_name] => daily
                        [rate] => 150.00
                     )
                  [1] => Array
                     (
                        [id] => 2
                        [building_type] => 1
                        [rate_name] => hourly
                        [rate] => 15.00
                      )
                )

        )
)

我知道Cake在使用模型關聯時可以輸出這樣的結果,但是我顯然無法正確地建立關聯,因為它繼續加入BuildingRentalRate.building_type上的Building.id(應該為Building.building_type = BuildingRentalRate.building_type)

即使和我這樣的交往...

 //Building.php
 public $hasMany = array('BuildingRentalRate' => array('foreignKey' => 'building_type'));

 //BuildingRentalRate.php
 public $belongsTo = array('Building' => array('foreignKey' => 'building_type'));

盡管在兩個模型中都將building_type指定為外鍵,但它將在BuildingRentalRate.building_type上加入Building.id。

我可以在條件中執行某種嵌套的SQL查詢,還是有一種更簡單的方法?

如果您在BuildingRentalRate.php這種方式設置關系,則應該能夠避免使用聯接

$public belongsTo = array(
    'Building' => array(
        'foreignKey' => false,
        'conditions' => array
        (
            'Building.building_type' => 'BuildingRentalRate.building_type'
        )
    )
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM