2012-05-05 64 views
0

我在學習如何顯示來自兩個表的信息。Kohana ORM顯示信息

表:

categories {category_id, category_title} 
forums {forum_id, forum_title} 
categories_forums {id_category, id_forum} 

型號:

class Model_Forum extends ORM { 

protected $_primary_key = 'forum_id'; 

protected $_belongs_to = array(
    'categories'=> array(
     'model' => 'category',     
     'through' => 'categories_forums',  
     'far_key' => 'id_category',   
     'foreign_key' => 'id_forum' 
    ), 
); 
} 

class Model_Category extends ORM { 

protected $_primary_key = 'category_id'; 

protected $_has_many = array(
    'forums'=> array(
     'model' => 'forum',     
     'through' => 'categories_forums',  
     'far_key' => 'id_forum',   
     'foreign_key' => 'id_category'  
    ), 
); 
} 

我不確定如何顯示。

到目前爲止,我有以下幾點:

$categories = ORM::factory('category'); 

$forums = $categories->forums->find_all(); 

我怎麼不顯示CATEGORY_ID,category_title,forum_id,FORUM_TITLE。

回答

0

以下似乎工作:

$categories = ORM::factory('category')->find_all(); 

$view = new View('default/index'); 

$view->categories = $categories; 

$this->response->body($view); 

foreach ($categories as $category) : 
echo $category->category_title; 
echo $category->category_id; 
foreach ($category->forums->find_all() as $forum) : 
echo $forum->forum_title; 
echo $forum->forum_id; 
endforeach; 
endforeach; 
0

您可以使用foreach循環,就像這樣:

$categories = ORM::factory('category'); 
foreach ($categories->find_all() as $category){ 
    echo $category->category_title, ' ', $category->id; 
}