doremifasodo0008008 2014-04-19 07:00
浏览 32
已采纳

CakePHP可包含和模型关联

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'
  • 写回答

1条回答 默认 最新

  • doudieyou5209 2014-04-19 07:49
    关注

    You have wrong relations between models. Replace them with:

    //Reservation model
    public $actsAs = array('Containable');
    public $hasMany = array('ReservationDetail', 'Payment');
    
    //ReservationDetail model
    public $actsAs = array('Containable');
    public $belongsTo = array('Reservation', 'Room');
    
    //Room model
    public $actsAs = array('Containable');
    public $hasMany = array('ReservationDetail');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值