Question

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    |                |
+---------+--------------+------+-----+---------+----------------+
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top