Kohana 中的 ORM 关系

发布于 2024-09-30 02:32:25 字数 2113 浏览 8 评论 0原文

我有以下表格:

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_eventsuser_summaries 都有外键来链接 usersevents 表。 我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2024-10-07 02:32:25

使用$user_event->user(不使用find())。模型看起来不错,但可能您应该为 usersummariesuserevents 关系手动定义相关模型(Ko3 inflector 对于这些名称可能会出现错误)

Use $user_event->user (without find()). Models look fine, but may be you should manually define related models for usersummaries and userevents relations (Ko3 inflector may works wrong for these names)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文