⚠ 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

set_relation_n_n filtering data displayed?



yepwingtim
  • profile picture
  • Member

Posted 12 January 2012 - 22:32 PM

I have a list of codes I use with set_relation_n_n

code_id, code_name, code_status

code_status is either active or inactive.

whenever i add or edit a record while choosing multiple code, i only want the ACTIVE code_status values to be displayed.

Is this possible?

Thanks

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 14 January 2012 - 09:17 AM

[quote name='yepwingtim' timestamp='1326407553' post='277']
I have a list of codes I use with set_relation_n_n

code_id, code_name, code_status

code_status is either active or inactive.

whenever i add or edit a record while choosing multiple code, i only want the ACTIVE code_status values to be displayed.

Is this possible?

Thanks
[/quote]

The only way to do it right now is with the set_model function. You can see an example of how to use it at: http://www.grocerycrud.com/crud/function_name/set_model

esuen
  • profile picture
  • Member

Posted 20 September 2012 - 06:00 AM

I want to do something like this.

I have 3 tables: company, user, and document.

company -> user is a 1:n relationship
company -> document is a 1:n relationship
user -> document is a n:n relationship

On the document table, I am using set_relation_n_n to establish the relationship with user. I want the unselected array of users to belong to the same company as the document. So is there a way to get the current row's data (from document table) so that I can do a custom query on the user table?

Thanks in advance.

esuen
  • profile picture
  • Member

Posted 21 September 2012 - 21:24 PM

I found my own solution.

I just extended grocery_CRUD_Model and modified the get_relation_n_n_unselected_array method with my custom query.


<?php
class my_document_model extends grocery_CRUD_Model
{
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);

$selection_primary_key = $this->get_primary_key($field_info->selection_table);
// Adding custom query code here
$document_id = end(explode('/', uri_string()));
$this->db->join('user_document', 'user_document.user_id = user.id');
$this->db->join('document', 'user_document.document_id = document.id');
$this->db->where('document.id', $document_id);
$this->db->where('document.company_id', 'user.company_id');
//-----
if($use_where_clause){
$this->db->where($field_info->where_clause);
}

if(!$use_template)
$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;
}
}
?>


Then in my controller I used set_model, to set it to my custom model.

Another question though, does the input type for selecting results change depending on how many results there are? I saw in the demo there are different pickers and wanted to know at what count does the input type change.