⚠ 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

using "where" in set_relation_n_n



jcasanova
  • profile picture
  • Member

Posted 11 June 2012 - 21:14 PM

How can do that? need to add a where with the set_relation_n_n.

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

Posted 11 June 2012 - 21:30 PM

For now this functionality is not available for grocery CRUD. You can only do it with the set_model function ( http://www.grocerycrud.com/documentation/options_functions/set_model ) . I have also an example of how to use it.

jcasanova
  • profile picture
  • Member

Posted 11 June 2012 - 21:46 PM

yeah I was looking that example, thanks.

jcasanova
  • profile picture
  • Member

Posted 12 June 2012 - 13:36 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?

jcasanova
  • profile picture
  • Member

Posted 12 June 2012 - 14:16 PM

[quote name='jcasanova' timestamp='1339508163' post='2261']
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;
}

}

Matt
  • profile picture
  • Member

Posted 12 July 2012 - 22:26 PM

Hi I didnt see this. Obviously not hacking the core is best but I thought I'd share anyway.

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