2012-07-12 52 views
1

我正在尋找特定於我的問題的幫助,因爲我似乎無法從Kohana文檔或Google搜索中獲得它。Kohana 3.3 ORM表加入

我有2個表:

contents
ID
URI
標題
template_id

templates
ID
標題

我試圖返回的所有數據對於contents行有匹配的 uri

目前我有兩種模式:內容和模板。

內容

class Model_Content extends ORM { 
protected $_table_name = 'contents'; 
protected $_has_one = array('template' => array()); 
.... 

模板

class Model_Template extends ORM { 

protected $_table_name = 'templates'; 
protected $_belongs_to = array(
    'content' => array() 
); 

然後在我的控制器:

$item = ORM::factory("Content")->get_by_uri($uri); 

指向:

$this->where("uri", "=", $uri)->find(); 

這將正確返回內容表中的數據,但是如何從模板表中獲取模板名稱?

感謝您的幫助。

回答

2

$item->template是你所需要的。不要忘記檢查模型的存在:if ($item->loaded())

+0

不應該是'$ this-> template-> find()'? – egis 2012-07-13 08:56:23

+1

否。一對一(和所屬)關係將自動加載。 – biakaveron 2012-07-13 10:50:37

+0

很好,謝謝! – dclawson 2012-07-17 20:50:18