cakephp HABTM - 列出与标签相关的帖子并显示每个帖子的其他标签

时间:2013-07-06 09:00:43

标签: cakephp has-and-belongs-to-many

我已经与帖子和标签表建立了拥有和属于多人(HABTM)的关系,其中tagpo是连接表。 我有一个场景,在标签视图中,我需要显示标签的所有帖子 - 这很好。问题部分是,我还需要为每个帖子显示其他标签。

我如何实现这一目标?

在Tags视图中,我有view.ctp,它有以下foreach循环。

foreach ($tag['Post'] as $post):
  echo $post['id']
  echo $post['title']
  echo $post['body']

  foreach ($post['Tag'] as $post):
    echo $post['tag_id']
    echo $post['tag_name']
  endforeach

endforeach  public function view($id = null) {
            $this->Tag->recursive = 1;

    $this->Tag->id = $id;
    if (!$this->Tag->exists()) {
        throw new NotFoundException(__('Invalid tag'));
    }
    $this->set('tag', $this->Tag->read(null, $id));

我在控制器中的所有视图功能都是 -

public function view($id = null) {
            $this->Tag->recursive = 1;

    $this->Tag->id = $id;
    if (!$this->Tag->exists()) {
        throw new NotFoundException(('Invalid tag'));
    }
    $this->set('tag', $this->Tag->read(null, $id));
}

第二个for循环给出了无法识别的索引'Tag'的错误。我需要在控制器中指定哪些关系,以便在“标记”视图中为每个帖子获取其他相关标记?

0 个答案:

没有答案
相关问题