简体   繁体   中英

best practice with CRUD system and JOIN queries in CodeIgniter

I'm using CodeIgniter to develop my app and I put together a very basic CRUD system (literally just create, retrieve, update & delete functions in my models) based on tutorial I found years ago (I forgot the URL). As I'm developing my app I realize I'm having to call the retrieve function of multiple models to get the data I need for a view (ie, get_product, get_category, get_buyer, etc.). I understand using a JOIN query is best for grabbing data from multiple tables, but in CodeIgniter with a CRUD model system where should JOIN queries live (controller, in the respective primary model or a separate model)?

You should handle queries in model section. This is a example join query in codeigniter using Active Records .

$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.

If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.

$this->db->join('comments', 'comments.id = blogs.id', 'left');

// Produces: LEFT JOIN comments ON comments.id = blogs.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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