[英]Kohana ORM Access Information
I'm trying to learn how to access and display information. 我正在尝试学习如何访问和显示信息。 I have categories, forums, topics, and posts. 我有类别,论坛,主题和帖子。 forums belongs to categories, topics belongs to forums, and posts belongs to topics. 论坛属于类别,主题属于论坛,帖子属于主题。 I don't know how to access posts. 我不知道如何访问帖子。
Tables: 表格:
categories {category_id, category_title}
forums {forum_id, forum_title}
categories_forums {id_category, id_forum}
topics {topic_id, topic_title}
forums_topics{id_forum, id_topic}
posts {post_id, post title}
topics_posts {id_topic, id_post}
Models: 楷模:
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'
),
);
}
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'
),
);
protected $_has_many = array(
'topics'=> array(
'model' => 'topic',
'through' => 'forums_topics',
'far_key' => 'id_topic',
'foreign_key' => 'id_forum'
),
);
}
class Model_Topic extends ORM {
protected $_primary_key = 'topic_id';
protected $_belongs_to = array(
'forums'=> array(
'model' => 'forum',
'through' => 'forums_topics',
'far_key' => 'id_forum',
'foreign_key' => 'id_topic'
),
);
protected $_has_many = array(
'posts'=> array(
'model' => 'post',
'through' => 'topics_posts',
'far_key' => 'id_post',
'foreign_key' => 'id_topic'
),
);
}
class Model_Post extends ORM {
protected $_primary_key = 'post_id';
protected $_belongs_to = array(
'topics'=> array(
'model' => 'topic',
'through' => 'topics_posts',
'far_key' => 'id_topic',
'foreign_key' => 'id_post'
),
);
}
So far I have the following: 到目前为止,我有以下内容:
foreach ($categories as $category) :
echo $category->category_title;
foreach ($category->forums->find_all() as $forum) :
echo $forum->forum_title;
$num_topics = $forum->topics->count_all(); echo $num_topics;
$num_posts = $forum->topics->posts->count_all(); echo $num_posts;
endforeach;
endforeach;
The problem is echo $num_posts displays 0. I'm guessing I'm accessing posts wrong. 问题是echo $ num_posts显示0。我猜我在访问帖子时出错。 How do I do it? 我该怎么做?
topics
to get the posts. 您必须迭代topics
才能获得帖子。 count_all()
Kohana reset the Model. 在count_all()
Kohana重置模型。 You must do it like this: 您必须这样做:
$topics = $forum->topics->find_all()->as_array();
$num_topics = count($topics);
foreach($topics as $topic)
{
$num_post_in_topic = $topic->posts->find_all()->count();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.