【问题标题】:CakePHP containables and model associationsCakePHP 可包含和模型关联
【发布时间】:2014-04-19 07:00:54
【问题描述】:

我是 CakePHP 的新手,我在here 发了一篇关于我在关系和查找数据方面遇到的问题。事实证明,我的关系似乎还不错,但可以使用可收容物进行搜索。

假设我有 3 个表:reservations、reservation_details 和具有以下数据的房间

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  

这是我的模型/协会

 //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'));

我正在尝试做的是搜索预订并返回reservation_details 和房间信息。现在所有的数据都被返回了,除了房间信息是一个空数组。这是我正在尝试进行的搜索

$reservation = $this->Reservation->find('all', array(
                                          'conditions' => array(
                                             'Reservation.guest_id' => $guest_id
                                           ),
                                           'contain' => array(
                                               'ReservationDetail' => array(
                                                        'Room'
                                                 )
                                             )
                                       ));

我相信 MySQL 查询会是这样的

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'

【问题讨论】:

  • 'foreignKey' = 'id' 有错别字,应该是 'foreignKey' => 'id'。也许您的代码中有相同的错字?
  • 在房间表上添加一个新字段为reservation_idforeign keyreservations 表.. 并且不需要为你们所有人添加Containable 行为模型......只需将此添加到 AppModel...
  • 尝试使用 bake 进行模型关联,它让你的生活更轻松
  • @FazalRasel 房间表只包含有关房间本身的信息...每个房间可用于多次预订。这就是为什么我在 reservation_details 中有一个 room_id。

标签: php mysql cakephp


【解决方案1】:

模型之间的关系有误。 将它们替换为:

//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');

【讨论】:

  • 谢谢,这行得通。我唯一要做的就是在 Room 模型中添加一个外键 ('ReservationDetail' => array('foreignKey' => 'room_id'))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
相关资源
最近更新 更多