Custom lookup tables

edited March 2014 in Modules
I've got so used to using Fuel's relationship table and configuration that I'd nearly forgotten how to create custom relationships in the Fuel admin.

I'm using the Ion Auth library again, and wanted to use the "multi" field type on the user-to-groups relationships.

So I looked up a similar thing I did months back, using v0.93 of Fuel. In the users_model form_fields() method I get the associated groups back from the look-up table "users_groups" by doing this:

$users_groups = array(); $CI = &get_instance(); $key = isset($values['id']) ? $values['id'] : NULL; ... $users_groups = array_keys($CI->users_groups_model->find_all_array_assoc('group_id', array('user_id' => $key)));

then build the field like so

$fields['groups'] = array('label' => 'Groups', 'type' => 'multi', 'model' => 'groups', 'value' => $users_groups, 'mode' => 'multi');

That still works great, but it isn't very v1.0. Can't this be defined as a relationship, only without using the fuel_relationships table, but the users_groups one instead - ie as a model property? I'm sure I have done that in v1.0 install, but I can't find where.

Comments

  • edited 11:47PM
    In your has_many or belongs_to relationships, you can specify the following additional parameters:
    relationships_model = a model for the lookup table,
    foreign_key = the key used for the foreign model
    candidate_key = the key used for current model to associate with

    Code may look something similar to this:
    public $has_many = array('categories' => array('model' => 'categories_model', 'module' => 'app', 'relationship_model' => 'my_relationships_model', 'foreign_key' => 'category_id', 'candidate_key' => 'my_id');
  • edited March 2014
    The (users_groups) look-up table must have the columns "candidate_table" and "foreign_table" present for this to work. Those columns are redundant for my purposes, but adding them with default values doesn't do any harm, and then that has_many expression works fine. I think that was an obstacle I'd forgotten about!

    Thanks!
  • edited 11:47PM
    Is this with the latest version of FUEL by chance?
  • edited 11:47PM
    Quite recent - in the last few weeks?
Sign In or Register to comment.