I'm new to CakePHP and made a post here about issues I was having with relations and finding data. It turned out that my relations appeared to be ok, but the search could be done using containables.
Say I have 3 tables: reservations, reservation_details and rooms with the following data
table reservations
id | confirmation_number | guest_id
1 123 1
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
3 1 2014-18-04 13:00 2 4.99
table rooms - an entry in reservation_details has one room_id
id | Name | Location
1 Room 1 building A
2 Room 2 building A
Here are my models/associations
//Reservation model
public $actsAs = array('Containable');
public $hasMany = array('ReservationDetail', 'Payment');
//ReservationDetail model
public $actsAs = array('Containable');
public $belongsTo = array('Reservation');
public $hasMany = array('Room' => array('foreignKey' = 'id'));
//Room model
public $actsAs = array('Containable');
public $belongsTo = array('ReservationDetail' => array('foreignKey' => 'room_id'));
What I'm trying to do is search for a reservation and also return the reservation_details and room information. Right now all of the data is being returned, except the for the room information which is an empty array. Here's the search I'm trying to do
$reservation = $this->Reservation->find('all', array(
'conditions' => array(
'Reservation.guest_id' => $guest_id
),
'contain' => array(
'ReservationDetail' => array(
'Room'
)
)
));
I believe the MySQL query would be something like
SELECT reservations.*, reservation_details.*, rooms.* from reservations
INNER JOIN reservation_details on reservation_details.reservation_id = reservation.id
INNER JOIN rooms on rooms.id = reservation_details.room_id
WHERE reservations.guest_id = '1'