⚠ 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

where on relation n_n ?



Christophe Conduché
  • profile picture
  • Member

Posted 19 April 2013 - 08:34 AM

Hello !

 

 

I want to filter the second table involved in a relation n_n.

 

 

        $this->grocery_crud->set_table('journee');
	$this->grocery_crud->columns('nom','date');
    	$this->grocery_crud->set_subject('Journée de saut');
 
	$this->grocery_crud->set_relation_n_n('Pilotes Tandem', 'journee_piltdm', 'piltdm', 'journee_id', 'piltdm_id', 'surnom');
	$this->grocery_crud->set_relation_n_n('VideoMan', 'journee_vdoman', 'vdoman', 'journee_id', 'vdoman_id', 'surnom');
	$this->grocery_crud->set_relation_n_n('pax', 'journee_pax', 'pax', 'journee_id', 'pax_id', 'billet');

For the relation with "pax", I want to be able to show only certain passengers, and so to put a where clause on the related table (on field date also present on "journee".

 

 

How to do this ?


davidoster
  • profile picture
  • Member

Posted 20 April 2013 - 08:00 AM

When the relation you want is a bit complex it is better to use a custom model in order to retrieve the values that you want to be displayed in a field.


Christophe Conduché
  • profile picture
  • Member

Posted 25 April 2013 - 13:30 PM

So, related to another post pointing on this doc (http://www.grocerycrud.com/documentation/options_functions/set_model) :

 

I have to define my custom model.

 

How to combine "normal" model for 2 of the 3 relations I have and the special one for 1 relation only (on pax in my context) ?

 

class My_Custom_model extends grocery_CRUD_Model  {
 
    function get_relation_n_n_unselected_array($field_info, $selected_values)
 
    {
        $selection_primary_key = $this->get_primary_key($field_info->selection_table);
 
        if($field_info->name = '....')
 
        {
            $this->db->where(....);
            .......your custom queries
        }
 
        $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
 
        $results = $this->db->get($field_info->selection_table)->result();
 
 
        $results_array = array();
        foreach($results as $row)
 
        {
            if(!isset($selected_values[$row->{$field_info->primary_key_alias_to_selection_table}]))
                $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_info->title_field_selection_table}; 
        }
 
 
        return $results_array;        
    }
 
}

 

Do you have a working sample of such a custom model ?


davidoster
  • profile picture
  • Member

Posted 26 April 2013 - 07:34 AM

When you extend the Grocery CRUD model essentialy what you is the one or all of the following things:

 

1. inherit (this is Object Oriented Programming - OOP) the code that exists on a class and you just want to change the way one/some function works,

e.g.

function get_relation_n_n_selection_array($primary_key_value, $field_info)

{
  // custom code
 
} - NOT RECOMMENDED
 
2. inherit as above but add one/some new function(s) that you want to use just like the example on http://www.grocerycrud.com/documentation/options_functions/set_model