【发布时间】:2010-07-01 19:16:50
【问题描述】:
我正在使用带有 ORM 的 Kohana 3.0.6。
我有一个名为“卡车”的模型,在他的表格中有一列带有他的制造商(“制造商”)的 ID。然后我在他的表中有带有 id 和名称的“制造商”模型。
我正在尝试在显示卡车列表时执行简单的 LEFT-JOIN,这样我就可以直接获取其制造商的名称。
这是我的“卡车”模型:
<?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),
);
}
如您所见,我正在使用这一行添加“has_one”,虽然我也在某处看到过“with()”调用,但无法使其正常工作(文档有点缺乏,特别是对于版本 3.x.x)。
protected $_has_one = array('maker' => array('model' => 'maker') );
这是我在视图中用来输出制造商名称的行(类似这些行):
foreach ($trucks as $t) {
echo($t->maker->name);
}
【问题讨论】: