⚠ 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 forum is read-only and soon will be archived. ⚠


set_relation 4th parameter


  • Please log in to reply
3 replies to this topic

#1 rolo2012

rolo2012

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 April 2012 - 08:30 PM

I have a table 'fuel' that have a relation whit 'car' and the cars have a group
I want to show only de cars of the group 2 (for example) and in the update/insert views
only let to add/modify cars in that group.In the update and in the insert views work fine
but in the list I show all the cars, not only the cars of group 2.

My code is:
$this->grocery_crud->set_table('fuel')
->set_relation('car','car','matricula',array("group"=>2));
I try :
->where('car.group','2')
but the name of the table car change internaly (Ej:car AS ferwr2)

#2 rolo2012

rolo2012

    Member

  • Members
  • PipPip
  • 12 posts

Posted 30 April 2012 - 02:05 PM

sorry If my English is not comprensible.
I want the crud operate only where car.group= 2 or other number
and it fails in the list view (show all).

If can be done extending the model I apreciate to.

#3 littleflow3r

littleflow3r

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 07 May 2012 - 09:35 PM

I have the same question here,

I have 2 tables,

Table1 structure => project_id, project_name

Table2 structure =>project_id, employee_id

This is n-to-n relationship. Now I want to show list data from Table1 where employee_id = x (In my case, I want to show all the project by employee_id = x ONLY )
How can I do this? I have tried the set_relation with 4th parameter (where in set_relation) but I dont know, it's still showed all of the data (not just the data where employee_id = x)

Please solution, thank you before :D

#4 rolo2012

rolo2012

    Member

  • Members
  • PipPip
  • 12 posts

Posted 08 May 2012 - 03:06 PM

I do it extending the model in the get_list I just copy the method and add some lines, line for get the unique name that asign the grocery crud in the join $vehiculo_fieldname=$unique_field_name; $vehiculo_joinname=$unique_join_name; and a where clause
$this->db->where("$vehiculo_joinname.grupo_trabajo",$this->grupo_trabajo);

And in the update and delete preforms some checks that only can operate in a grupo_trabajo (group_id its strores in seccion)

<?php
// application/models/grocery_crud_vehiculo_model.php
class grocery_crud_vehiculo_model  extends grocery_CRUD_Model  {
	protected $grupo_trabajo=-1;
  
	function __construct()
	{
		$ci=&get_instance();
		$this->grupo_trabajo=$ci->session->userdata('grupo_trabajo');
		parent::__construct();
	}
  
  
	function get_list()
	{
	 if($this->table_name === null)
	  return false;
	
	 $select = "{$this->table_name}.*";	
		$vehiculo_fieldname='';
		$vehiculo_joinname='';
	  
	 if(!empty($this->relation))
	  foreach($this->relation as $relation)
	  {
	   list($field_name , $related_table , $related_field_title) = $relation;					  
	   $unique_join_name = $this->_unique_join_name($field_name);
	   $unique_field_name = $this->_unique_field_name($field_name);
	  
   if(strstr($related_field_title,'{'))
		$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
	   else	  
		$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
	  
	   if($this->field_exists($related_field_title))
		$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
					if($related_table=='vehiculo'){
						$vehiculo_fieldname=$unique_field_name;
						$vehiculo_joinname=$unique_join_name;
					}
	  }
	 $this->db->select($select, false);	
	  
		$this->db->where("$vehiculo_joinname.grupo_trabajo",$this->grupo_trabajo);
	 $results = $this->db->get($this->table_name)->result();
	 //echo $this->db->last_query();
		//echo "$vehiculo_fieldname $vehiculo_joinname";
	 return $results;
	}
  
 
	function db_update($post_array, $primary_key_value)
	{	  
		$primary_key_field = $this->get_primary_key();
		$this->db->from($this->table_name);
		$this->db->join('vehiculo',"vehiculo.id={$this->table_name}.vehiculo");
		$this->db->where($this->table_name.".".$primary_key_field,$primary_key_value);
		$this->db->where('vehiculo.grupo_trabajo',$this->grupo_trabajo);
		if($this->db->count_all_results()<=0)return false;
		$this->db->from('vehiculo');	  
		$this->db->where('grupo_trabajo',$this->grupo_trabajo);
		$this->db->where('vehiculo.id',$post_array['vehiculo']);	  
		if($this->db->count_all_results()<=0)return false;
	  
		return parent::db_update($post_array, $primary_key_value);
	}  
  
	function db_insert($post_array)
	{
		$this->db->from('vehiculo');	  
		$this->db->where('grupo_trabajo',$this->grupo_trabajo);
		$this->db->where('vehiculo.id',$post_array['vehiculo']);	  
		if($this->db->count_all_results()<=0){				
			return false;				
		}
		return parent::db_insert($post_array);	  
	}
  
	function db_delete($primary_key_value)
	{
		$primary_key_field = $this->get_primary_key();
		$this->db->from($this->table_name);
		$this->db->join('vehiculo',"vehiculo.id={$this->table_name}.vehiculo");
		$this->db->where($this->table_name.".".$primary_key_field,$primary_key_value);
		$this->db->where('vehiculo.grupo_trabajo',$this->grupo_trabajo);
		if($this->db->count_all_results()<=0){	
			//echo $this->db->last_query();
			return false;				
		}
	  
		return parent::db_delete($primary_key_value);
	}

}
In the controller i use:
$this->grocery_crud->set_model('grocery_crud_vehiculo_model');
				$this->grocery_crud
				 ->set_relation('vehiculo','vehiculo','matricula',
						 array("vehiculo.grupo_trabajo"=>$this->session->userdata('grupo_trabajo')));
The 4th parameter only restrict the add/edit combo

It's work fine for me.
I have 2 tables
The table vehiculo has a grupo_trabajo_id and only operate in the group that i set.
And supplies that have a vehicle_id
The code has some spanish words tell me if you don't understand.
I hope to be useful




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users