⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Need to filter relation dropdown



TechDaddies-Kevin
  • profile picture
  • Member

Posted 21 November 2012 - 20:13 PM

I have 3 tables:

Events
id|title|owner_id

Users
id|email address

Users_Groups
group_id|user_id


On my events CRUD form, I need the dropdown to show users (This works great) but I need the list to only show users who belong to group_id 1 in the Users_Groups table.

How would I accomplish this? I believe we need to use the set_model method, but I'm having a difficult time understanding the documentation in that regard.

joshyro
  • profile picture
  • Member

Posted 22 November 2012 - 06:09 AM

I think this can be done with database relations, look this example http://www.grocerycrud.com/examples/set_a_relation

TechDaddies-Kevin
  • profile picture
  • Member

Posted 22 November 2012 - 06:22 AM

We are already using the set relation method. The problem is that we need to filter the list of related items by using a third table.

Kobus Myburgh
  • profile picture
  • Member

Posted 22 November 2012 - 15:00 PM

Hi, just throwing an idea out to you - I have not tested this but maybe this helps.

From the documentation it seems you can not add a table, but it should be possible to use tablename.fieldname in a $crud->where() call.

If that works, it may be a hint to update the documentation for that?

Regards,

Kobus

TechDaddies-Kevin
  • profile picture
  • Member

Posted 26 November 2012 - 19:53 PM

That almost works, but not quite. When I use the where clause in my set_relation call, the following query is run:

SELECT * FROM (`users`) WHERE `users_groups`.`group_id` = 1 AND users_groups.user_id = users.id

This would work perfectly if I could change that FROM `users` to be FROM `users`, `users_groups`

dropcase
  • profile picture
  • Member

Posted 30 November 2012 - 20:32 PM

Are you looking for something like a join?

/topic/264-join-tables/#entry1018

Kobus
  • profile picture
  • Member

Posted 31 December 2012 - 13:14 PM

Kevin,

Did you manage? You could also set a relation, that will automatically create the correct join, when in conjuntion with the $crud->where() clause, you can probably do what you wanted to.

Please let us know?