dongyuqie4322 2012-07-20 19:37
浏览 34
已采纳

Kohana 3.2,ORM has_many,belongs_to关系没有引用正确的密钥

I have two tables named utilities and types that i am trying to access the other fields within types from the utilities view. e.g; $utilities->type->type;

Utilities would have_one type and type would belong_to utility right?

This is how i have it currently, and rather than referencing utilities.type_id its setting the key as utilities.id. As a result its pulling different types for each row, even though they are all of the same type.

enter image description here

How should i lay my models relationships out using Kohana 3.2 ORM.

Utilities

<?php
class Model_Utility extends ORM {
    protected $_has_one = array(
        'type' => array(
            'model' => 'type',
            'foreign_key' => 'type_id',
        ),
    );

Table Structure

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

Types

class Model_Type extends ORM {
    protected $_belongs_to = array(
        'utility' => array(
        )
    );

Table Structure

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| type_id | int(11)      | NO   | PRI | NULL    | auto_increment |
| type    | varchar(255) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
  • 写回答

1条回答 默认 最新

  • dougangxin5695 2012-07-24 07:38
    关注

    Utility belongs_to Type (utilities has a type_id key), and Type has_many utilities.

    PS. Also you need to define a PK name for types table ($_primary_key property).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳