dongyoufo5672 2013-08-10 19:39
浏览 45
已采纳

CakePHP显示HABTM关联

I'm new to CakePHP and just want to display a list of associated tags in a post's view.

I have searched all over the web and nothing seems to work.

This is what I have at the moment:

// PostController
public function view($id = null) {
    $this->set('tags', $this->Post->Tag->find('all', array('conditions' => array('PostTag.post_id' => $id))));

    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $post = $this->Post->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post'));
    }
    $this->set('post', $post);
}

// Post's view.ctp
echo $this->Text->toList($tags);

This is the error I'm getting:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'PostTag.post_id' in 'where clause'

This should be so easy but I am completely stuck.

Thanks to anyone who can help!

  • 写回答

2条回答 默认 最新

  • douhui1957 2013-08-11 22:34
    关注

    Finally! After finding another question here about joining tables and some experimentation, I got it to work.

    public function view($id = null) {
        $options = array(
            'joins' => array(
                array('table' => 'posts_tags',
                    'alias' => 'PostTags',
                    'type' => 'left',
                    'conditions' => array(
                        'Post.id = PostTags.post_id'
                    )
                ),
                array('table' => 'tags',
                    'alias' => 'Tag',
                    'type' => 'left',
                    'conditions' => array(
                        'PostTags.tag_id = Tag.id'
                    )
                )
            )
            ,'conditions' => array(
                'PostTags.post_id' => $id
            )
            ,'fields' => array(
                'Tag.title' // the name of the tag
            )
            ,'recursive' => -1
        );
    
        $tagsList = $this->Post->find('all', $options); 
    
        // added $result and foreach to ensure that all results would be printed
        $result = array();
        foreach ($tagsList as $tag):
            array_push($result, $tag['Tag']['title']);
        endforeach;
    
        $this->set('tags', $result);
    
        // ... rest of the call to find the post info
    }
    
    // Post's view.ctp
    echo $this->Text->toList($tags);
    

    Before I added $result, it would only print out the first association. I used "echo pr($tags);" in my view and found that the results I wanted were nested inside two arrays. After I added the foreach, it would correctly list all of the assigned tags in my view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等