⚠ 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

How do I change what is displayed after set_relation_n_n has been called?



westerhold

westerhold
  • profile picture
  • Member

Posted 15 March 2012 - 19:58 PM

I have a situation that is very similar to this which makes a call to set_relation_n_n:

[color=#0000ff]$crud[/color]->[color=#006600]set_relation_n_n[/color][color=#66cc66]([/color][color=#ff0000]'actors'[/color], [color=#ff0000]'film_actor'[/color], [color=#ff0000]'actor'[/color], [color=#ff0000]'film_id'[/color], [color=#ff0000]'actor_id'[/color], [color=#ff0000]'fullname'[/color],[color=#ff0000]'priority'[/color][color=#66cc66])[/color];

As can be seen in the example the actor's [b]fullname [/b]is pulled from the actor table and displayed in the selection box as seen below:

[b]AKROYD CHRISTIAN[/b]
[b]AKROYD DEBBIE[/b]
[b]AKROYD KIRSTEN[/b]
[b]ALLEN CUBA[/b]
[b]ALLEN KIM[/b]

What I want is to append the actor's name with some additional information which I pull from some other table. Let us imagine that other table is called [b]actor_phone_number_relation[/b] and is structured like this:

[b]actor_phone_number_relation(table)
actor_phone_number_id INT(11)
actor_id INT(11)
phone_number VARCHAR(10)[/b]

With this information how can I append the actor's name with their phone number as seen below?

[b]AKROYD CHRISTIAN : 555-123-7890[/b]
[b]AKROYD DEBBIE : 555-123-5435[/b]
[b]AKROYD KIRSTEN : 555-123-0123[/b]
[b]ALLEN CUBA : 555-654-3210[/b]
[b]ALLEN KIM : 555-123-9317[/b]

web-johnny

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

Posted 19 March 2012 - 07:19 AM

This can only be done with the set_model method. For more you can see an example at: http://www.grocerycrud.com/documentation/options_functions/set_model and /topic/90-how-to-create-an-extension-for-grocery-crud-real-example-included/

westerhold

westerhold
  • profile picture
  • Member

Posted 05 April 2012 - 15:01 PM

Thanks for your reply, it got me on the right track. For my specific problem I just wanted to change what was displayed. This required overriding grocery_crud's [b]get_relation_n_n_unselected_array[/b] function (which was done in the first example you linked to) and grocery_crud's [b]get_relation_n_n_selection_array[/b] function. Using the same scenario from my original post this is how I over rode those functions:

[php]<?php
class grocery_model_extension extends grocery_Model {
function grocery_model_extension() {
parent::__construct();
}
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}" );
}
$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 ) {
if( $field_info->field_name == 'actors' ) {
$query = $this->db->query( 'SELECT phone_number FROM actor_phone_number_relation WHERE actor_id = ' . $row->$selection_primary_key . ' )' );
$phone_number = $query->result();
$phone_number = $phone_number[ 0 ]->phone_number;
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table} . ' : ' . $phone_number;
} else {
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table};
}
}
return $results_array;
}
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}" );
$results = $this->db->get( $field_info->selection_table )->result();
$results_array = array( );
foreach( $results as $row ) {
if( !isset( $selected_values[ $row->$selection_primary_key ] ) ) {
if( $field_info->field_name == 'actors' ) {
$query = $this->db->query( 'SELECT phone_number FROM actor_phone_number_relation WHERE actor_id = ' . $row->$selection_primary_key . ' )' );
$phone_number = $query->result();
$phone_number = $phone_number[ 0 ]->phone_number;
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table} . ' : ' . $phone_number;
} else {
$results_array[ $row->$selection_primary_key ] = $row->{$field_info->title_field_selection_table};
}
}
}
return $results_array;
}
}
?>[/php]

Juan Carlos

Juan Carlos
  • profile picture
  • Member

Posted 02 December 2014 - 17:38 PM

Thanks for your reply, it got me on the right track. For my specific problem I just wanted to change what was displayed. This required overriding grocery_crud's get_relation_n_n_unselected_array function (which was done in the first example you linked to) and grocery_crud's get_relation_n_n_selection_array function. Using the same scenario from my original post this is how I over rode those functions:

[php]<?php
class grocery_model_extension extends grocery_Model {
function grocery_model_extension() {
parent::__construct();
}
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}" );
}
$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 ) {
if( $field_info->field_name == 'actors' ) {
$query = $this->db->query( 'SELECT phone_number FROM actor_phone_number_relation WHERE actor_id = ' . $row->$selection_primary_key . ' )' );
$phone_number = $query->result();
$phone_number = $phone_number[ 0 ]->phone_number;
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table} . ' : ' . $phone_number;
} else {
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table};
}
}
return $results_array;
}
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}" );
$results = $this->db->get( $field_info->selection_table )->result();
$results_array = array( );
foreach( $results as $row ) {
if( !isset( $selected_values[ $row->$selection_primary_key ] ) ) {
if( $field_info->field_name == 'actors' ) {
$query = $this->db->query( 'SELECT phone_number FROM actor_phone_number_relation WHERE actor_id = ' . $row->$selection_primary_key . ' )' );
$phone_number = $query->result();
$phone_number = $phone_number[ 0 ]->phone_number;
$results_array[ $row->{$field_info->primary_key_alias_to_selection_table} ] = $row->{$field_info->title_field_selection_table} . ' : ' . $phone_number;
} else {
$results_array[ $row->$selection_primary_key ] = $row->{$field_info->title_field_selection_table};
}
}
}
return $results_array;
}
}
?>[/php]

 

Hello Newbie,

 

I find it interesting your solution, I have a similar problem.

 
You can show your complete example ?.
 
or at least you can show me how is your controller?