我正在尝试学习如何从两个表中显示信息。
表:
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 :(得分:0)
您可以使用foreach循环,如下所示:
$categories = ORM::factory('category');
foreach ($categories->find_all() as $category){
echo $category->category_title, ' ', $category->id;
}
答案 1 :(得分: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;