dongshubang7816 2010-01-28 23:15
浏览 61

如何在Kohana 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.

  • 写回答

2条回答 默认 最新

  • douji4948 2010-01-29 00:13
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程