如何在 Kohana ORM 中关联具有不同外键名称的表?

发布于 2024-08-19 12:29:16 字数 3089 浏览 6 评论 0原文

我正在构建一个 Kohaha 应用程序来管理星号中的 sip 线路。

我想使用 ORM,但我想知道如何关联某些已经建立好的表。

例如表 sip_lines 看起来像这样。

+--------------------+------------------+------+-----+-------------------+-----------------------------+
| Field              | Type             | Null | Key | Default           | Extra                       |
+--------------------+------------------+------+-----+-------------------+-----------------------------+
| id                 | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| sip_name           | varchar(80)      | NO   | UNI | NULL              |                             |
| displayname        | varchar(48)      | NO   |     | NULL              |                             |
| line_num           | varchar(10)      | NO   | MUL | NULL              |                             |
| model              | varchar(12)      | NO   | MUL | NULL              |                             |
| mac                | varchar(16)      | NO   | MUL | NULL              |                             |
| areacode           | varchar(6)       | NO   | MUL | NULL              |                             |
| per_line_astpp_acc | tinyint(1)       | NO   |     | 0                 |                             |
| play_warning       | tinyint(1)       | NO   |     | 0                 |                             |
| callout_disabled   | tinyint(1)       | NO   |     | 0                 |                             |
| notes              | varchar(80)      | NO   |     | NULL              |                             |
| last_update        | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------------+------------------+------+-----+-------------------+-----------------------------+

sip_buddies 是这样的:

+----------------+------------------------------+------+-----+-----------+----------------+
| Field          | Type                         | Null | Key | Default   | Extra          |
+----------------+------------------------------+------+-----+-----------+----------------+
| id             | int(11)                      | NO   | PRI | NULL      | auto_increment | 
| name           | varchar(80)                  | NO   | UNI |           |                | 
| host           | varchar(31)                  | NO   |     |           |                | |                |
| lastms         | int(11)                      | NO   |     | 0         
*** snip ***
+----------------+------------------------------+------+-----+-----------+----------------+

这两个表实际上是相关的,如 sip_lines.sip_name = sip_buddies.name

我如何在 Kohana ORM 中将它们关联起来,因为这不太正确,不是吗?

<?php defined('SYSPATH') or die('No direct script access.');

/* A model for all the account information */
class Sip_Line_Model extends ORM
{
    protected $has_one = array("sip_buddies");
}

?>

编辑:实际上,可以公平地说这些表与外键没有正确相关!哦。

编辑:看起来 Kohana ORM 并不那么灵活。 ORM 可能不是最佳选择,并且最适合数据模型可以更改的全新项目。原因是键名称必须遵循特定的命名约定,否则它们将不相关 在科哈纳。

I'm building a Kohaha application to manage sip lines in asterisk.

I'm wanting to use ORM but I'm wondering how do relate certain tables that are already well established.

e.g. the table sip_lines looks like this.

+--------------------+------------------+------+-----+-------------------+-----------------------------+
| Field              | Type             | Null | Key | Default           | Extra                       |
+--------------------+------------------+------+-----+-------------------+-----------------------------+
| id                 | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| sip_name           | varchar(80)      | NO   | UNI | NULL              |                             |
| displayname        | varchar(48)      | NO   |     | NULL              |                             |
| line_num           | varchar(10)      | NO   | MUL | NULL              |                             |
| model              | varchar(12)      | NO   | MUL | NULL              |                             |
| mac                | varchar(16)      | NO   | MUL | NULL              |                             |
| areacode           | varchar(6)       | NO   | MUL | NULL              |                             |
| per_line_astpp_acc | tinyint(1)       | NO   |     | 0                 |                             |
| play_warning       | tinyint(1)       | NO   |     | 0                 |                             |
| callout_disabled   | tinyint(1)       | NO   |     | 0                 |                             |
| notes              | varchar(80)      | NO   |     | NULL              |                             |
| last_update        | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------------+------------------+------+-----+-------------------+-----------------------------+

sip_buddies is this:

+----------------+------------------------------+------+-----+-----------+----------------+
| Field          | Type                         | Null | Key | Default   | Extra          |
+----------------+------------------------------+------+-----+-----------+----------------+
| id             | int(11)                      | NO   | PRI | NULL      | auto_increment | 
| name           | varchar(80)                  | NO   | UNI |           |                | 
| host           | varchar(31)                  | NO   |     |           |                | |                |
| lastms         | int(11)                      | NO   |     | 0         
*** snip ***
+----------------+------------------------------+------+-----+-----------+----------------+

The two tables are actually related as sip_lines.sip_name = sip_buddies.name

How do I relate them in Kohana ORM as this wouldn't be quite right would it?

<?php defined('SYSPATH') or die('No direct script access.');

/* A model for all the account information */
class Sip_Line_Model extends ORM
{
    protected $has_one = array("sip_buddies");
}

?>

EDIT: Actually, it'd be fair to say that these tables are not properly related with foreign keys! doh.

EDIT: Looks like Kohana ORM is not that flexible. ORM is probably not the way to go and works best for completely new projects where the data model can be altered. The reason being that the key names must follow a specific naming convention or else they won't relate
in Kohana.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

对你再特殊 2024-08-26 12:29:16

这不取决于 kohana ORM,而是取决于您的数据库设计。使用整数而不是 varchar 作为外键一直是一条“规则”,我已经很久没有见过这样的设计了。

添加整数 FK 或根本不考虑使用 ORM,那样行不通。

编辑:

是的,如果你真的想这样做的话;

protected $has_one = array('buddy' => array('model' => 'sip_buddy', 'foreign_key' => 'name' ) );

It's not up to kohana ORM but up to your db design. It's always been a "rule" to use integer as foreign key and not varchar, I haven't seen a design like this for ages.

Add integer FKs or don't think of using ORM at all, just doesn't work that way.

EDIT:

And yes, if you really want to do it this way;

protected $has_one = array('buddy' => array('model' => 'sip_buddy', 'foreign_key' => 'name' ) );
木森分化 2024-08-26 12:29:16

仅当您计划使用一对一关系时,这才是正确的。但是,如果您计划建立一对多关系,则需要使用 $has_many。根据您的需要,您可以选择为另一个表创建一个模型,并为该表使用 $belongs_to 。

这一切都在这里解释: http://docs.kohanaphp.com/libraries/orm/starting

It's correct only if you plan on using a one-to-one relationship. However, if you plan on having one-to-many relationships, you'll want to use $has_many. Depending on your needs you may optionally create a model for the other table and use $belongs_to for that one.

It's all explained here: http://docs.kohanaphp.com/libraries/orm/starting

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