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