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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?