繁体   English   中英

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

[英]best practice with CRUD system and JOIN queries in CodeIgniter

我使用CodeIgniter开发应用程序,并根据几年前发现的教程(我忘记了URL)组装了一个非常基本的CRUD系统(实际上只是在模型中创建,检索,更新和删除函数)。 在开发应用程序时,我意识到我必须调用多个模型的检索函数来获取视图所需的数据(即,get_product,get_category,get_buyer等)。 我知道使用JOIN查询最适合从多个表中获取数据,但是在带有CRUD模型系统的CodeIgniter中,JOIN查询应该存在于哪里(控制器,在相应的主模型中还是在单独的模型中)?

您应该在模型部分中处理查询。 这是使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM