English 中文(简体)
How do I relate tables with different foreign key names in 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.

问题回答

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

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签