如何在Kohana ORM中将具有不同外键名称的表关联起来? [英] How do I relate tables with different foreign key names in Kohana ORM?

查看:148
本文介绍了如何在Kohana ORM中将具有不同外键名称的表关联起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Kohaha应用程序来管理星号中的饮水机行.

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

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

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

例如表格sip_lines看起来像这样.

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是这样的:

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 ***
+----------------+------------------------------+------+-----+-----------+----------------+

这两个表实际上与sip_lines.sip_name = sip_buddies.name相关

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

我怎么在Kohana ORM中将它们联系起来,因为这不太正确吗?

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");
}

?>

实际上,可以公平地说,这些表与外键没有正确地关联!嗯.

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

看起来Kohana ORM不太灵活.对于可以更改数据模型的全新项目,ORM可能不是最好的选择.原因是关键字名称必须遵循特定的命名约定,否则它们将不相关 在Kohana.

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.

推荐答案

这不取决于kohana ORM,而取决于您的数据库设计.使用整数作为外键而不使用varchar一直是一个规则",我很久没见过这样的设计了.

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.

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

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

是的,如果您真的想用这种方式;

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

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

这篇关于如何在Kohana ORM中将具有不同外键名称的表关联起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆