首页 文章

在CodeIgniter中使用CRUD系统和JOIN查询的最佳实践

提问于
浏览
0

我正在使用CodeIgniter来开发我的应用程序,我根据几年前发现的教程(我忘记了URL)整理了一个非常基本的CRUD系统(字面意思是在我的模型中创建,检索,更新和删除函数) . 当我正在开发我的应用程序时,我意识到我必须调用多个模型的检索功能来获取视图所需的数据(即get_product,get_category,get_buyer等) . 我理解使用JOIN查询最适合从多个表中获取数据,但在CodeIgniter中使用CRUD模型系统应该JOIN查询实时(控制器,在相应的主模型或单独的模型中)?

1 回答

  • 0

    您应该在模型部分处理查询 . 这是使用Active Records的codeigniter中的示例连接查询 .

    $this->db->join();
    
    Permits you to write the JOIN portion of your query:
    
    $this->db->select('*');
    $this->db->from('blogs');
    $this->db->join('comments', 'comments.id = blogs.id');
    
    $query = $this->db->get();
    
    // Produces: 
    // SELECT * FROM blogs
    // JOIN comments ON comments.id = blogs.id
    Multiple function calls can be made if you need several joins in one query.
    

    如果需要特定类型的JOIN,可以通过函数的第三个参数指定它 . 选项包括:左,右,外,内,左外和右外 .

    $this->db->join('comments', 'comments.id = blogs.id', 'left');
    
    // Produces: LEFT JOIN comments ON comments.id = blogs.id
    

相关问题