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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置