Take the 2-minute tour ×

I have 2 tables

Users Table => id , username
OrdersTable => order_id,user_id, qty,total

I need to link these two tables with .

id and user_id.

how can i do this with grocery CRUD.

share|improve this question
Any luck solving this? –  Ben Swinburne Dec 7 '11 at 7:33
nope. Bypassed Grocery_crud altogether for CRUD in orders table –  soden Dec 7 '11 at 8:56
Presumably you tried the solutions in my answer? –  Ben Swinburne Dec 7 '11 at 9:04
Not really. Just browsed through it. Thought doing it through custom code would be much easier and got on with it. –  soden Dec 7 '11 at 9:07

2 Answers 2

up vote 2 down vote accepted

If you're using OrdersTable as your primary table, you can use a 1:1 relationship using set_relation(). This would be if you were viewing an order and wanted to see/set which user the order belongs to.


This will allow you to select which user for a particular order.

However, presumably a user may have many orders- a 1:n relationship. This would be the case if UsersTable is your primary table, i.e. you're viewing a user in the CRUD and want to see all associated orders.

This method requires an intermediary table, with which you link the two existing tables, and a slight change to your existing table structure. You'd need to remove user_id in the OrdersTable. The intermediary table, for the sake of argument called UserOrdersTable would require the following columns.

user_id INT
order_id INT
priority INT

You can then add the relationship using the set_relation_n_n() method.

You can then reverse the set_relation_n_n() call for when viewing the OrdersTable so you can see which user made an order.

To summarise, a 1:n relationship using set_relation_n_n() will allow you to view orders by a user, and a user for an order.

share|improve this answer

If you mean query, then;

select u.id, u.username, o.order_id, o.qty, o.total
from users u
left join orders o on(o.user_id = u.id)
where 1=1
share|improve this answer
I meant using the grocerycrud.com codeigniter CRUD library –  soden Dec 4 '11 at 8:47

Your Answer


