⚠ 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

to set_relation_n_n or not?



ezgoen

ezgoen
  • profile picture
  • Member

Posted 20 May 2013 - 01:23 AM

Hi Folks,

 

I'm in a dilemma - which way to go

 

The scenario

 

Table Items

 

Table Sales

 

I want to be able to bulk add items to a sale

with a relation table items-sales I can do this BUT and this is a BIG BUT!!

 

I DONT want to be able to select items that are already assigned to another sale

with set_relation_n_n.

 

Sooooo 

 

I could set some constraints - and disallow  adding the sale item twice

but thats still confusing to the user - they would wonder why cant they add an item they select.

un selectable items should not be visible at all.

 

So I can use the mutiselct field and populate the array in a callback - but thats not the interface

users want - they want to be able to bulk-select items not add one at a time.

 

I guess I'm hinting at using functionality like that in get_relation_n_n_input

 

 

 

any suggestions??

 

 

cheers

 

Ez


ezgoen

ezgoen
  • profile picture
  • Member

Posted 20 May 2013 - 02:23 AM

I wont say I solved my problem - but I did hack around it.

 

If anyone has any suggestions for a cleaner way to do this - I'm open to suggestions.

 

I created a custom_gc_model with the following code:

function get_relation_n_n_already_used_array($field_info)
    {
    	$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(" ", " ", $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);
    	 
    	if(empty($field_info->priority_field_relation_table))
    	{
    		if(!$use_template){
    			$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
    		}
    	}
    	else
    	{
    		$this->db->order_by("{$field_info->relation_table}.{$field_info->priority_field_relation_table}");
    	}
    	$this->db->join(
    			$field_info->selection_table,
    			"{$field_info->relation_table}.{$field_info->primary_key_alias_to_selection_table} = {$field_info->selection_table}.{$selection_primary_key}"
    		);
    	$results = $this->db->get($field_info->relation_table)->result();
    	
    	$results_array = array();
    	foreach($results as $row)
    	{
    		$results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_name_hash};
    	}
    			 
    	return $results_array;
    }
	
	
	
	function get_relation_n_n_unselected_array($field_info, $selected_values)
    {
	
		$already_used=$this->get_relation_n_n_already_used_array($field_info);
	
    	$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(" ", " ", $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);
        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($already_used[$row->$selection_primary_key]))
                $results_array[$row->$selection_primary_key] = $row->{$field_name_hash};
        }
        
        return $results_array;       
    }

Your thoughts ??

 

 

Cheers

 

Ez


davidoster

davidoster
  • profile picture
  • Member

Posted 20 May 2013 - 11:02 AM

I have a simple application that I do this,

"SELECT `id`, `activities_id`, `description` FROM `groups` WHERE `activities_id` IN (SELECT `id` FROM (`activities`) WHERE `description` LIKE 'H-%' ORDER BY `id`) ORDER BY `id` DESC";

 

 

Can't you limit the same way (using of course a custome model) the items so they are displayed only the ones you need?