set_relation_n_n

void set_relation_n_n(string $field_name, string $relation_table, string $selection_table, string $primary_key_alias_to_this_table, string $primary_key_alias_to_selection_table , string $title_field_selection_table [ , string $priority_field_relation [, string $where_clause]]
Quick Description: Sets a relation with n-n relationship.

Sets a relation with n-n relationship. There must include 3 tables. The basic table, the selection table and the relation table (or else middle table).



Basically we have 6 arguments as follow:
  1. $field_name: Is the name of the field. This field name is NOT at the database so don't worry too much about it, we need just one for the user to understand what the list is for.
  2. $relation_table: Is the junction table (Wikipedia) that it relates the two tables.
  3. $selection_table: The selection table is the table that the junction table is referring to. For example if the junction table is referring to film_id the user need to specify in which table the id will be found. Once this is done grocery CRUD automatically is referring to the PRIMARY KEY of the other table
  4. $primary_key_alias_to_this_table: This is the field name that is referring to our table at the junction table. For example at the above example the film_id is the one that referring to our table (film)
  5. $primary_key_alias_to_selection_table: This is the field name that is referring to the relation table at the junction table. For example at the above example the actor_id is the one that referring to the relation table (actor)
  6. $title_field_selection_table: This is the field name of the related table that it is a human readable title that will be displayed to the end user. In our example the fullname is the field name of the actor that can be recognised
  7. $priority_field_relation: (optional) This field is not required. If you really care about the priority that the data at the junction table are stored, you need to specify a priority (or ordering) field to store the data with the priority in it. Have in mind that the UI is changing if you add this value
  8. $where_clause: (optional) This is the where statement in case you need to specify an extra where statement to the query of relations.

Example:

function film_management()
{
    $crud = new grocery_CRUD();
 
    $crud->set_table('film');
    $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
    $crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');
 
    $crud->unset_columns('description','special_features','last_update');
    $crud->fields('title', 'description', 'actors' ,  'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
 
    $output = $crud->render();
 
    $this->_example_output($output);
}    
Note: The below example is an iframe so it might appeared with a scroll bar. If you like you can view the example on a new tab