Kohana 中的 ORM 关系
我有以下表格:
users { id, name }
events { id, name }
user_events { id, date, user_id, event_id }
user_summaries { id, user_id, event_id, qty }
表user_events
存储所有用户事件(不同时间有许多相同类型),user_summaries
存储每种类型事件发生的数量每个用户。前者仅获取 INSERT
查询,而后者则主要获取 UPDATE
查询。
当然,user_events
和 user_summaries
都有外键来链接 users
和 events
表。 我想在 Kohana 的模型中表示这种关系。我有使用外键的经验,但这是我接触 Kohana 和 ORM 的第一个方法,我不确定如何定义我的模型来表示这些关系。
我想要做的基本上是这样的:
// I'm able to do this.
$user = ORM::factory('user', 1);
$user_events = $user->events->find_all();
$user_summaries = $user->usersummaries->find_all();
// I'm able to do this, too.
$event = ORM::factory('event', 1);
$event_users = $event->users->get_all();
// I don't know how to be able to do this:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
$event_details = $user_event->event->find();
我当前的模型如下所示:
# classes/model/user.php
class Model_User extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/event.php
class Model_Event extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/userevent.php
class Model_Userevent extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
# classes/model/usersummary.php
class Model_Usersummary extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
当前调用:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
始终返回 users
表中的第一个用户,即使 userevent
有不同的用户身份。 我想我需要更改 Model_userevent
中的某些内容,但我会在一些帮助下使用。
注意。我正在使用 Kohana 3.0.8。
I have the following tables:
users { id, name }
events { id, name }
user_events { id, date, user_id, event_id }
user_summaries { id, user_id, event_id, qty }
Table user_events
stores all user events (there are many of the same types at different times) and user_summaries
stores how many of each type of events occured to each user. The former gets only INSERT
queries and the latter mostly UPDATE
ones.
Of course both user_events
and user_summaries
have foreign keys to link with users
and events
tables.
I want to represent this relation in my Models in Kohana. I have experience with foreign keys, but it's my first approach to both Kohana and ORM and I'm not sure really how to define my models there to represent these relations.
What I want to be able to do is basically this:
// I'm able to do this.
$user = ORM::factory('user', 1);
$user_events = $user->events->find_all();
$user_summaries = $user->usersummaries->find_all();
// I'm able to do this, too.
$event = ORM::factory('event', 1);
$event_users = $event->users->get_all();
// I don't know how to be able to do this:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
$event_details = $user_event->event->find();
My current models look like this:
# classes/model/user.php
class Model_User extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/event.php
class Model_Event extends ORM
{
protected $_has_many = array(
'scores' => array(),
'usersummaries' => array(),
);
}
# classes/model/userevent.php
class Model_Userevent extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
# classes/model/usersummary.php
class Model_Usersummary extends ORM
{
protected $_belongs_to = array(
'user' => array(),
'event' => array(),
);
}
Currently calling:
$user_event = ORM::factory('userevent', 1);
$user_details = $user_event->user->find();
returns always the first user from users
table, even though userevent
has different user_id.
I presume I need to change something in Model_userevent
but I'd use with some help.
NB. I'm using Kohana 3.0.8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
$user_event->user
(不使用find()
)。模型看起来不错,但可能您应该为usersummaries
和userevents
关系手动定义相关模型(Ko3 inflector 对于这些名称可能会出现错误)Use
$user_event->user
(withoutfind()
). Models look fine, but may be you should manually define related models forusersummaries
anduserevents
relations (Ko3 inflector may works wrong for these names)