如何在Codeigniter 3模型中获取类别,项目和子项目?

时间:2019-05-07 13:31:53

标签: database codeigniter categories

我刚刚开始在Codeigniter中构建Web应用程序,但是我无法解决模型中嵌套查询的问题。

我有三个表:

类别,链接和主题。每个链接和每个主题只能有一个类别,而每个类别可以有多个链接或主题。

每个链接可以有一个主题(或根本没有一个主题),每个主题可以有一个或多个链接。

表结构看起来像这样(缩短):

Categories:

 - ID
 - DESCR

Links:

- ID
- DESCR
- ID_CATEGORY
- ID_TOPIC (either an ID from the Topics-Table or NULL)

Topics:

- ID
- DESCR
- ID_CATEGORY

In my View, i want to display them in the following manner:

- Category 1
 - Link 1
 - Link 2
 - Topic 1
     - Topiclink 1
     - Topiclink 2
 - Link 3

- Category 2
 - Topic 2
     - Topiclink 3
     - Topiclink 4
 - Topic 3
     - Topiclink 5
     - Topiclink 6
 - Link 4

因此,将有3个嵌套查询。

我的模型如下:

public function get_categories(){

    //1. Get the categories

    $this->db->select("                   
    ec.ID_CATEGORY, 
    c.DESCR,
    c.VISIBLE,
    i.KEYVALUE");
    $this->db->from("episode_categories ec");
    $this->db->join("categories c", "c.ID = ec.ID_CATEGORY");
    $this->db->join("ini i", "i.SETTING = c.VISIBLE");
    $this->db->where("i.KEYWORD", "CATEGORY_VISIBLE");
    $this->db->where("ec.ID_EPISODE", $_SESSION['cur_episode']);
    $categories = $this->db->get()->result_array();  

    //2. Get the Entries (Topics and Links with NULL as TOPIC_ID)

    foreach($categories as $key => $value)
    {
        $sql_links="SELECT 
        pf_users.USERNAME,
        pf_users.NAME_SHOW,
        pf_links.ID AS ID,
        pf_links.URL AS URL,
        pf_links.ID_USER AS ID_USER,
        pf_links.ID_EPISODE,
        pf_links.ID_CATEGORY,
        pf_links.DESCR,
        NULL AS IS_TOPIC,
        pf_links.REIHENF,
        pf_links.DONE,
        pf_links.DONE_TS,
        pf_links.INFO AS INFO,
        pf_episoden.DONE AS EPISODE_DONE 

        from pf_links 
        JOIN pf_users on pf_users.ID = pf_links.ID_USER 
        JOIN pf_episoden on pf_episoden.ID = pf_links.ID_EPISODE 

        WHERE ID_EPISODE = ".$_SESSION['cur_episode']." AND ID_CATEGORY = ".$value['ID_CATEGORY']." AND ID_TOPIC IS NULL 

        UNION ALL SELECT pf_users.USERNAME,

        pf_users.NAME_SHOW,
        pf_topics.ID AS ID,
        NULL AS URL,
        pf_topics.ID_USER AS ID_USER,
        pf_topics.ID_EPISODE,
        pf_topics.ID_CATEGORY,
        pf_topics.DESCR,
        1 AS IS_TOPIC,
        pf_topics.REIHENF,
        pf_topics.DONE,
        pf_topics.DONE_TS,
        pf_topics.INFO AS INFO,
        pf_episoden.DONE AS EPISODE_DONE 
        from pf_topics 
        JOIN pf_users on pf_users.ID = pf_topics.ID_USER 
        JOIN pf_episoden on pf_episoden.ID = pf_topics.ID_EPISODE 
        WHERE ID_EPISODE = ".$_SESSION['cur_episode']." AND ID_CATEGORY = ".$value['ID_CATEGORY']." ORDER BY REIHENF,
        ID ASC";
        $query_links = $this->db->query($sql_links);
        $categories[$key]['link_list'] = $query_links->result_array(); 

         //3. Get the Topiclinks (Links with an ID_TOPIC set)

         foreach( $categories[$key]['link_list'] as $key2 => $value2)
         {
              if($value2['IS_TOPIC'] == 1)
              {
                   $sql_topic_links = "SELECT * FROM pf_links WHERE ID_TOPIC = ".$value2['ID'];
                   $query_topic_links = $this->db->query($sql_topic_links);
                   $categories[$key2]['topic_links'] = $query_topic_links->result();                            
              }
         }
    }
    return $categories;
}

一切正常,直到主题的链接(代码中的第3步)为止。我得到的数组对于每个主题链接都与可能的ID_TOPIC中的最后一个相同。因此,主题内的链接都是相同的。

在该功能的第三步中,我需要更改什么才能仅获取特定主题的链接?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我能够解决此问题:

在步骤3中,以下行

$categories[$key2]['topic_links'] = $query_topic_links->result();                            

必须更改为:

 $categories[$key]['link_list'][$key2]['topic_links'] = $query_topic_links->result_array(); 

所以我认为我可以遍历结果集。