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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀