⚠ 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

Ordering on relation n_n



archerwisdom
  • profile picture
  • Member

Posted 06 November 2013 - 16:18 PM

Hi,

 

I had been looking for the answer from few topics but still clueless.

I want to have a list of relation n_n order by the disha_id.How to do that ?

 

Here is my code:

 $crud->set_relation_n_n('disha',  'order_disha', 'disha', 'order_id', 'disha_id', 'disha_name','priority');

Here are my tables:

 

order_disha

- disha_id

- order_id

- priority

 

disha

- disha_id

- dish_name

 

order

- order_id

- function_name


jinbatsu
  • profile picture
  • Member

Posted 09 January 2014 - 14:41 PM

yes, me too, how can we do like that?

anyone please?


jinbatsu
  • profile picture
  • Member

Posted 09 January 2014 - 15:55 PM

I have try like this.
 
You can extend or edit application/model/grocery_crud_model.php
Find function get_relation_n_n_unselected_array (at line +/- 349)
 
Replace with this one:
 
 <?php

    function get_relation_n_n_unselected_array($field_info, $selected_values)
    {
     $use_where_clause = !empty($field_info->where_clause);

     $select = "";
     $related_field_title = $field_info->title_field_selection_table;
     $use_template = strpos($related_field_title,'{') !== false;
     $field_name_hash = $this->_unique_field_name($related_field_title);

     if($use_template)
     {
     $related_field_title = str_replace(" ", "&nbsp;", $related_field_title);
     $select .= "CONCAT('".str_replace(array('{','}'),array("',COALESCE(",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $field_name_hash";
     }
     else
     {
     $select .= "$related_field_title as $field_name_hash";
     }
     $this->db->select('*, '.$select,false);

     if($use_where_clause){
     $this->db->where($field_info->where_clause);
     }

     $selection_primary_key = $this->get_primary_key($field_info->selection_table);
        // jinbatsu edit: sort order for using template {field_name}
        if(!$use_template) {
         $this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
        } else {
            preg_match_all('/\{([A-Za-z0-9_]+?)\}/', $field_info->title_field_selection_table, $out);
            $field_title_sort_order_arr = $out['1'];
            foreach ($field_title_sort_order_arr as $field_title_sort_order ) {
                //$field_title_sort_order = str_replace(array('{','}'), '', $field_title_sort_order);

                $this->db->order_by("{$field_info->selection_table}.{$field_title_sort_order}");
            }
           // $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->$selection_primary_key]))
                $results_array[$row->$selection_primary_key] = $row->{$field_name_hash};
        }

        return $results_array;
    }

?>
 
This will use sort order by using template, like this example:
 
 $crud->set_relation_n_n('disha',  'order_disha', 'disha', 'order_id', 'disha_id', '{disha_id} {disha_name}','priority');
 
will generate:
order_by('disha_id')
order_by('disha_name')