dotn30471 2013-08-13 18:43
浏览 31
已采纳

在CakePHP中设置模型关系中的键

I'm pulling data from a table called Visits and I want to get room information as well. The cell Visits.room_id references Rooms.id, however, CakePHP is using Visits.id instead which I'm assuming is because it's the primary key.

Here are the tables

Visits - id is primary key. I even tried setting room_id as a foreign key when creating the table
id    |   guest_id    |    room_id   |   arrival_date   |    departure_date

Rooms - id is prmary key. 
id  |   description   |   max_occupancy

So basically it's doing the following in a query

where Visits.id = Rooms.id

Instead of

where Visits.room_id = Rooms.id

In my Visit Model I have the following

public $hasOne = array(
    'room' => array(
        'className' => 'room',
        'foreignKey' => 'id'
    )
);

And here's an example of a query from the VisitsController

$visits = $this->paginate('Visit', array(
            'arrival_date >=' => $date
            )
        );                  
        $this->set(compact('visits'));

Is there any way I can tell CakePHP to use Visit.room_id instead of Visit.id?

  • 写回答

2条回答 默认 最新

  • duanpuluan0480 2013-08-13 18:56
    关注

    Try this in your Visit model

    public $belongsTo = array('Room');
    

    instead of hasOne.

    Maybe this is what you want?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题