SQL join with Kohana ORM
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);
}
Do you have the column named truck_id in the makers table?
精彩评论