using "where" in set_relation_n_n
- Single Page
Posted 11 June 2012 - 21:14 PM
Posted 11 June 2012 - 21:30 PM
Posted 11 June 2012 - 21:46 PM
Posted 12 June 2012 - 13:36 PM
Posted 12 June 2012 - 14:16 PM
johnny,I figured out, where to use the "where" but do you know how can I set "where X is null" with the $this->db->where() style?
[/quote]
Found it!
if any one needs it it goes like this, usign set_model
class Grocery_Custom_model extends grocery_CRUD_Model {
function get_relation_n_n_selection_array($primary_key_value, $field_info)
{
$selection_primary_key = $this->get_primary_key($field_info->selection_table);
if(empty($field_info->priority_field_relation_table))
{
$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}");
}
if($field_info->relation_table == 'transformador_trafonet'){ //<--- here is the relation table, in order to run only with that relation and not with ALL relation_n_n
$this->db->where("{$field_info->relation_table}.fecha_fin is null", null); // <--- fecha_fin is null , was the where clause used.
}
$this->db->where($field_info->primary_key_alias_to_this_table, $primary_key_value);
$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_info->title_field_selection_table};
}
return $results_array;
}
}
Posted 12 July 2012 - 22:26 PM
In grocery_crud I added a where clause
public function set_relation_n_n($field_name, $relation_table, $selection_table, $primary_key_alias_to_this_table, $primary_key_alias_to_selection_table , $title_field_selection_table, $priority_field_relation_table = null, $where_clause = false)
{
$this->relation_n_n[$field_name] =
(object)array(
'field_name' => $field_name,
'relation_table' => $relation_table,
'selection_table' => $selection_table,
'primary_key_alias_to_this_table' => $primary_key_alias_to_this_table,
'primary_key_alias_to_selection_table' => $primary_key_alias_to_selection_table ,
'title_field_selection_table' => $title_field_selection_table ,
'priority_field_relation_table' => $priority_field_relation_table,
'where_clause_selection_table' => $where_clause
);
return $this;
}
Then in the model
function get_relation_n_n_unselected_array($field_info, $selected_values)
{
$selection_primary_key = $this->get_primary_key($field_info->selection_table);
$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");
if($field_info->where_clause_selection_table !== false) $this->db->where($field_info->where_clause_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_info->title_field_selection_table};
}
return $results_array;
}
I think this deserves a place in the core to make it match the set_relation_n api