A common issue I run into when building CRUD applications is how to retrieve many-to-many relationships from the database and put them into an object model.
Say, for example, I have the following data structure:
customers:
- name: bob
items:
- chair
- hat
- name: steve
items:
- hat
- table
This is stored in the database like so:
customers
id | name
---|------
0 | bob
1 | steve
items
id | name
---|------
0 | chair
1 | table
2 | hat
customers_items_link
customer_id | item_id
------------|--------
0 | 0
0 | 2
1 | 2
1 | 1
When it comes time to turn the data in the database back into objects in my programming language of choice, currently I'm pulling all three tables individually and processing them afterwards to create the objects and link them. Is there any way to reconstruct the objects in one query?
select * from customers, items, customers_items_link