0

情報にアクセスして表示する方法を学ぼうとしています。カテゴリ、フォーラム、トピック、および投稿があります。フォーラムはカテゴリに属し、トピックはフォーラムに属し、投稿はトピックに属します。投稿へのアクセス方法がわかりません。

テーブル:

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}

モデル:

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'   
    ),
  );
}

これまでのところ、次のものがあります。

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;

問題は echo $num_posts が 0 を表示することです。投稿へのアクセスが間違っていると思います。どうすればいいのですか?

4

1 に答える 1

0
  1. topics投稿を取得するには反復する必要があります。
  2. count_all()Kohana がモデルをリセットした後。

次のようにする必要があります。

    $topics = $forum->topics->find_all()->as_array();

    $num_topics = count($topics);

    foreach($topics as $topic) 
    {
        $num_post_in_topic = $topic->posts->find_all()->count();
    }
于 2012-05-09T07:57:54.000 に答える