Kohana 2 ORM 自定义主键生成错误
在我的数据库中,我有一个 jobs
表、一个 details
表和一个 employers
表。
jobs
和 details
是一对一的关系,employers
和 details
是一对一的关系一对多关系。并非所有工作都有详细信息,但所有详细信息都有一个雇主。
我认为从 details
中删除 id
字段并使用 job_id
字段作为主键是有意义的。数据库引擎是 InnoDB,因此主键连接应该快如闪电。
因此,在更改表并在 details
模型中添加主键声明后,我仍然可以通过 $job->detail
访问详细信息。但是,这行代码:
$employer = $job->detail->employer->name;
生成错误:
未定义索引:detail_job_id
除了自定义主键之外,我没有更改任何内容,所以我知道这是问题的根源,但我以前从未使用过自定义主键,所以我不确定是什么导致了错误或我的原因需要修复。
有人使用带有自定义主键的 Kohana2 ORM 吗?
In my DB I have a jobs
table, a details
table, and an employers
table.
jobs
and details
are in a one-to-one relationship, and employers
and details
are in a one-to-many relationship. Not all jobs have details, but all details have one employer.
I thought it would make sense to drop the id
field from details
, and use the job_id
field as the primary key. The DB engine is InnoDB, so primary key joins ought to be lightning fast.
So after changing the table and adding the primary key declaration in the details
model I can still access details via $job->detail
. However, this line of code:
$employer = $job->detail->employer->name;
generates an error:
Undefined index: detail_job_id
I changed nothing aside from the custom primary key, so I know this is the source of the problem, but I've never used a custom primary key before, so I am not sure what is causing the error or what I need to fix.
Anyone used the Kohana2 ORM with custom primary keys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIR,在 v2.3.4 中,您可以使用
$foreign_key
属性为相关模型定义 FK。类似于protected $foreign_key = array('employer' => 'detail_id');
AFAIR, in v2.3.4 you can use
$foreign_key
property to define FK for related models. Something likeprotected $foreign_key = array('employer' => 'detail_id');