Welcome Guest, Not a member yet? Register   Sign In
Using Model's CRUD methods in advanced queries
#1

Hi all,
I have created 3 tables in my database.

Code:
Products (ID, name)
Categories (ID, name)
Products_Categories (productID, categoryID)

This last table is used to link one or more categories to one or more products.
Can I use CRUD methods available in CI 4 to get, for example, all products in a given category?
Thanks for your help.
Reply
#2

(This post was last modified: 10-30-2019, 07:59 AM by edica.)

Yes. You can also use other Query Builder Class functions.

https://codeigniter4.github.io/CodeIgnit...ilder.html
Reply
#3

Hi RobT,

What you're asking for is more about ORM than CRUD.
CI4 comes with some basic CRUD / ORM tools (see https://codeigniter4.github.io/userguide...model.html)

But many-to-many relationships are more complex than it seems, especially if there more than 2 entities implied in the association or if there are some additional attributes in the association.

I've made a try with CI3 in order to manage these kind of associations (plus the possibility to link to a concrete or abstract Model)
You can find it here: https://www.concorde-project.org

Have a good day,
Vincent
Reply
#4

Thanks for your reply, @edica and @vincent78.
Reply
#5

I have a module to add some basic relationship loading to CodeIgniter 4. Please read through the docs before use. https://github.com/tattersoftware/codeig...-relations

Quick start

1. Install with Composer: > composer require tatter/relations

2. Extend the model: class ProductModel extends \Tatter\Relations\Model

3. Load relations:
$products = $productModel->with('categories')->findAll();
var_dump($products->categories);
Reply
#6

Thank you, @MGatner.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB