tags:

views:

62

answers:

1

I'm using Kohana 3.0.6 with ORM.

I have a model named "truck" and in his table there's a column with the id of his maker ("maker"). Then I have the "maker" model with the id and the name in his table.

I'm trying to do a simple LEFT-JOIN when I display the listing of the trucks so I can get directly the names of their maker.

Here is my "truck" model:

    <?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Truck extends ORM {
 // Database settings
    protected $_db = 'default';
    protected $_table_name = 'trucks';
    protected $_primary_key = 'id';

    //Tried adding this but doesn't seems to work
 protected $_has_one = array('maker' => array('model' => 'maker') );

 // Table fields
 protected $_table_columns = array(
        'id'  => array('data_type' => 'int', 'is_nullable' => FALSE),
        'serial' => array('data_type' => 'string', 'is_nullable' => FALSE),
        'maker'  => array('data_type' => 'string', 'is_nullable' => FALSE),
        'model'  => array('data_type' => 'string', 'is_nullable' => FALSE),
        'year'  => array('data_type' => 'int', 'is_nullable' => FALSE),
    );

}

As you can see, I'm using this line to add the "has_one", though I've also seen the "with()" call somewhere but couldn't make it work proprely (doc is a bit lacking, especially for version 3.x.x).

 protected $_has_one = array('maker' => array('model' => 'maker') );

Here's the line I'm using in the view to output the maker name (something along those lines):

foreach ($trucks as $t) {
    echo($t->maker->name);
}
A: 

Do you have the column named truck_id in the makers table?

slacker
No since there is not one truck per maker... but one maker per truck. And I have a column named "maker" in the Trucks table, and I identified it in the "has_one" arra, unless I mistken.
Ceb
If your Truck has_one Maker, then the foreign key goes in the Maker table. If your Truck belongs_to Maker, then the foreign key goes in the Truck table. Your code above states that Truck has_one Maker, that's why I asked. I think you should change your models so that Truck belongs_to Maker and Maker has_many Trucks.
slacker