具有分组和嵌套数组的 CakePHP 自定义数据对象
我正在开发一个具有广泛数据库关系的应用程序,我需要返回一个特定的数据对象。我遇到麻烦了。这就是我需要的样子:
AccommodationGroup =>
array(
['name']=>'Group1',
['AccommodationRoom']=> array(
[0]=> array(
['id']=>1,
['RoomIdentifier']=>'Cabin001',
),
[1]=> array(
['id']=>2,
['RoomIdentifier']=>'Cabin002'
)
)
)
AccommodationRoom 通过camp_id 与营地相关。 AccommodationGroup 通过 Accommodation_group_id 与 AccommodationRoom 相关。
正如您在数据对象示例中所看到的,我需要检索特定营地的所有组,其中每个组的房间作为该组的嵌套数组,并且所有这些都仅限于特定营地。
我尝试通过使用 findAllByCampId() 选择所有适用的组来实现这一点,当然,这不起作用(知道它不会起作用,但无论如何都尝试了)。另一个表 AccommodationRoomsCamp 是 AccommodationRooms 和 Camps 之间的过渡表。一种方法是:
$this->AccommodationRoomsCamp->findAllByCampId($id, array('group'=>'AccommodationRoomsCamp.accommodation_group_id'))
但 Accommodation_group_id 未存储在 AccommodationRoomsCamp 中,因为它已被存储在 AccommodationRoom 表中。我认为我需要做不止一项手术,但我很困惑。有点新手。有想法吗?
I'm working on an app with extensive database relationships and I need to return a specific data object. I'm having trouble with it. Here's what I need it to look like:
AccommodationGroup =>
array(
['name']=>'Group1',
['AccommodationRoom']=> array(
[0]=> array(
['id']=>1,
['RoomIdentifier']=>'Cabin001',
),
[1]=> array(
['id']=>2,
['RoomIdentifier']=>'Cabin002'
)
)
)
AccommodationRoom is related to camp by camp_id. AccommodationGroup is related to AccommodationRoom by accommodation_group_id.
As you can see in the data object example, I need to retrieve all the groups for a particular camp with the rooms of each group as a nested array of the group, and all this restricted to a particular camp.
I've tried to get at it by selecting all the applicable groups using findAllByCampId() which, of course, doesn't work (knew it wouldn't but tried it anyway). Another table, AccommodationRoomsCamp is a transitional table between AccommodationRooms and Camps. One way to do it would be:
$this->AccommodationRoomsCamp->findAllByCampId($id, array('group'=>'AccommodationRoomsCamp.accommodation_group_id'))
but accommodation_group_id is not stored in AccommodationRoomsCamp because it is already being stored in the AccommodationRoom table. I think that I need to do more than one operation, but I'm baffled. Little bit of a newb. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遵循表的相关方式有点困难 - 但我认为您可以使用递归查找来解决您的问题。您的控制器中类似这样的内容:
$this->AccomodationRoomsCamp->recursive = 2;
$this->AccomodationRoomsCamp->find('all, array('conditions' => array(bla bla bla bla
参见 http://book.cakephp.org/view/1063/recursive
Have a little bit of a hard time following how your tables are related - but I think you can solve your problem using recursive finds. Something like this in your controller:
$this->AccomodationRoomsCamp->recursive = 2;
$this->AccomodationRoomsCamp->find('all, array('conditions' => array(bla bla bla bla
See http://book.cakephp.org/view/1063/recursive