Php 在Codeigniter中将标记表连接到posts表

Php 在Codeigniter中将标记表连接到posts表,php,mysql,codeigniter,tags,Php,Mysql,Codeigniter,Tags,我需要一些帮助来检索属于特定标记组的帖子。我正在使用Codeigniter和MySQL 我的桌子是这样的: 帖子-帖子、标题、日期等 标签-tagid,标签 post_标签-postid,tagid 这是我在模型中使用的函数: function get_posts($tags) { $this->db->select('*'); $this->db->from('posts'); $this->db->order_by('date',

我需要一些帮助来检索属于特定标记组的帖子。我正在使用Codeigniter和MySQL

我的桌子是这样的:

帖子-帖子、标题、日期等

标签-tagid,标签

post_标签-postid,tagid

这是我在模型中使用的函数:

function get_posts($tags) {

    $this->db->select('*');
    $this->db->from('posts');
    $this->db->order_by('date', 'DESC');
    $this->db->join('post_tags', 'posts.postid = post_tags.postid');
    $this->db->join('tags', 'post_tags.tagid = tags.tagid');
    $this->db->where_in('tags.tag', $tags);
    $q = $this->db->get();
    return $q->result();
}
到目前为止,查询正在以我想要的方式运行,并返回所有正确的帖子

但是,在循环遍历结果并设置特定的post信息时,每个post只列出第一个标记

是否可以重新构造查询,以便每篇文章都包含与原始$tags数组匹配的所有标记


谢谢

我还没有对此进行测试,但我认为您需要向查询中添加一个连接类型:

function get_posts($tags) {
    $this->db->select('*');
    $this->db->from('posts');
    $this->db->order_by('date', 'DESC');
    $this->db->join('post_tags', 'posts.postid = post_tags.postid');

    // add the "right" keyword to produce a RIGHT JOIN
    $this->db->join('tags', 'post_tags.tagid = tags.tagid', 'right'); 

    $this->db->where_in('tags.tag', $tags);
    $q = $this->db->get();
    return $q->result();
}

这将产生完全相同的结果。生成的查询是什么?您可以尝试几个单独的关键字:
左内
左外
右内
、或
右外
。此外,如果您的应用程序刚开始时距离不太远,您可以考虑使用ORM。我使用Datamapper ORM(),这个查询会很简单:
$posts->include_related('tag','tags')->order_by('date','DESC')->get()我尝试了每种类型的联接,但是每次返回的post数组数据都完全相同。您可以发布正在生成的实际查询吗?