1

我正在使用 Cake 2.3.8 并且在有效地搜索数据时遇到了一些麻烦。我正在检索我正在寻找的数据,但也有很多我不需要的东西,因为可包含。

例如说我有以下表格

table reservations
id  |  confirmation_number   |   guest_id
1         123                        1
2         345                        2

table reservation_details -a reservation can have multiple entries (multiple rooms)
id  |  reservation_id   |   date     |  time   | room_id    |   rate
 2           1            2014-18-04    13:00        1           9.99
 4           1            2014-18-04    14:00        2           4.99
 5           2            2014-19-04    13:00        2           4.99

有以下型号

 //Reservation model
 public $actsAs = array('Containable');
 public $hasMany = array('ReservationDetail');

 //ReservationDetail model
 public $actsAs = array('Containable');
 public $belongsTo = array('Reservation');

现在搜索日期大于或等于 19 日的预订。日期存储在 reservation_details 表中。

$reservations = $this->Reservation->find('all', array(
                                        'conditions' => array(
                                            'Reservation.guest_id' => '1'
                                        ),
                                        'contain' => array(
                                              'ReservationDetail' => array(
                                                 'conditions' => array(
                                                     'ReservationDetail.date >=' => '2014-19-04'
                                                )
                                            )
                                        )

                                   ));

此搜索将返回 Reservation #1,即 ReservationDetail 的空数组,用于保留 #1。问题是我不想要任何这些信息。如果 ReservationDetail 条件与我的搜索匹配,我只希望返回数据。现在,如果没有找到匹配项,它只会为 ReservationDetail 返回一个空数组。

上述搜索的结果将类似于

Array(
    [0] => array   //I don't want this index to be returned at all because the reservation_details weren't met

    (
         [Reservation] => array
             (
                  id => 1
                  confirmation_number => 123
                  guest_id => 1
             )
         [ReservationDetail] => array
             (
             )

     )

     [1] => array

     (
         [Reservation] => array
             (
                  id => 2
                  confirmation_number => 345
                  guest_id => 2
             )
         [ReservationDetail] => array
             (
                  id => 5
                  reservation_id => 2
                  date => 2014-19-04
                  time => 13:00
                  room_id => 2
                  rate => 4.99 
             )
      )
)

如您所见,找到的第一个结果的 ReservationDetail 不符合我的标准。它仍然返回 Reservation 表和 ReservationDetail 的空数组。

如果 ReservationDetail 不符合我的条件,是否有其他方法可以搜索以便根本不会返回预订表?

4

1 回答 1

1
$reservations = $this->Reservation->find('all', array(
    'conditions' => array(
        'Reservation.guest_id' => 1,
        'ReservationDetail.date >=' => '2014-19-04'
    ),
    'joins' => array(
        array(
            'table' => 'reservation_details',
            'alias' => 'ReservationDetail',
            'type' => 'inner',
            'conditions' => array(
                'ReservationDetail.reservation_id = Reservation.id'
            )
        )
    ),
    'contain' => array(
        'ReservationDetail'
    )
));
于 2014-04-20T19:08:39.363 回答