I'm using Cake 2.3.8 and having a bit of trouble searching for data efficiently. I'm retrieving the data I'm looking for, but also a lot of stuff I don't need due to containables.
For example Say I have the following tables
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
With the following models
//Reservation model
public $actsAs = array('Containable');
public $hasMany = array('ReservationDetail');
//ReservationDetail model
public $actsAs = array('Containable');
public $belongsTo = array('Reservation');
Now perform a search for reservations where the date is greater or equal to the 19th. The date is stored in the reservation_details table.
$reservations = $this->Reservation->find('all', array(
'conditions' => array(
'Reservation.guest_id' => '1'
),
'contain' => array(
'ReservationDetail' => array(
'conditions' => array(
'ReservationDetail.date >=' => '2014-19-04'
)
)
)
));
This search would return Reservation #1, an empty array for ReservationDetail for reservation #1. The issue is I don't want any of that information. I only want data to be returned if the ReservationDetail conditions matches my search. Right now, it's only returning an empty array for ReservationDetail if the match isn't found.
The result of the above search would be something like
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
)
)
)
As you can see, the ReservationDetail for the first result found did not meet my criteria. It still returns Reservation table and an empty array for ReservationDetail.
Is there some other way I can search so that the reservation table won't be returned at all if the ReservationDetail doesn't match my criteria?
$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'
)
));