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 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下