0

我正在尝试学习如何显示两个表格中的信息。

表:

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。

4

2 回答 2

0

您可以使用 foreach 循环,如下所示:

$categories = ORM::factory('category');
foreach ($categories->find_all() as $category){
  echo $category->category_title, ' ', $category->id;
}
于 2012-05-05T10:05:12.700 回答
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;
于 2012-05-05T21:51:50.340 回答