How to link tables with different foreign key names in Kohana ORH?

I am creating a Kohaha application for managing sip lines in an asterisk.

I want to use ORM, but I'm wondering how to link specific tables that are already installed.

eg. The sip_lines table is as follows.

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

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

How can I link them in Kohana ORH, as that would be wrong, wouldn't 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 would be fair to say that these tables are not related to foreign keys! Doh.

EDIT: It seems that Kohana ORM is not so flexible. ORM is probably not the way to go and works best for brand new projects where the data model can be changed. The reason is that key names must conform to a specific naming convention, otherwise they will not be related in Cohan.

+1
source share
2 answers

This is not up to the Kohana ORM, but up to your db design. It was always a β€œrule” to use the whole as a foreign key, not varchar, I have not seen such a design for ages.

Add integer FKs or don't think about using ORM at all, it just doesn't work.

EDIT:

And yes, if you really want to do this,

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

This is only correct if you plan to use a one-to-one relationship. However, if you plan on having a one-to-many relationship, you will want to use $ has_many. Depending on your needs, you can optionally create a model for another table and use $ belongs_to for this.

Everything is explained here: http://docs.kohanaphp.com/libraries/orm/starting

0
source

Source: https://habr.com/ru/post/1310000/


All Articles