[英]Kohana 3.2, ORM has_many, belongs_to relationship not referencing the correct key
I have two tables named utilities
and types
that i am trying to access the other fields within types from the utilities view. 我有两个名为
utilities
和types
表,我试图从实用程序视图访问类型中的其他字段。 eg; 例如;
$utilities->type->type;
Utilities would have_one
type and type would belong_to
utility right? 公用事业有
have_one
类型和类型belong_to
实用工具权?
This is how i have it currently, and rather than referencing utilities.type_id
its setting the key as utilities.id
. 这就是我目前的方式,而不是引用
utilities.type_id
其设置为utilities.id
。 As a result its pulling different types for each row, even though they are all of the same type. 因此,它为每一行拉出不同的类型,即使它们都是相同的类型。
How should i lay my models relationships out using Kohana 3.2 ORM. 我应该如何使用Kohana 3.2 ORM建立我的模型关系。
<?php
class Model_Utility extends ORM {
protected $_has_one = array(
'type' => array(
'model' => 'type',
'foreign_key' => 'type_id',
),
);
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| utility_name | varchar(255) | NO | | NULL | |
| type_id | int(11) | NO | | NULL | |
| contact_name | varchar(255) | NO | | NULL | |
| contact_email | varchar(255) | NO | | NULL | |
| contact_phone | varchar(255) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
class Model_Type extends ORM {
protected $_belongs_to = array(
'utility' => array(
)
);
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| type_id | int(11) | NO | PRI | NULL | auto_increment |
| type | varchar(255) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
Utility belongs_to
Type ( utilities
has a type_id
key), and Type has_many
utilities. Utility
belongs_to
Type( utilities
具有type_id
键)和Type has_many
实用程序。
PS. PS。 Also you need to define a PK name for
types
table ( $_primary_key
property). 您还需要为
types
表定义PK名称( $_primary_key
属性)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.