Jump to content


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

set_relation_n_n

  • Please log in to reply
3 replies to this topic

#1 westerhold

westerhold

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 15 March 2012 - 07:58 PM

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

$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');

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

AKROYD CHRISTIAN
AKROYD DEBBIE
AKROYD KIRSTEN
ALLEN CUBA
ALLEN KIM

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 actor_phone_number_relation and is structured like this:

actor_phone_number_relation(table)
actor_phone_number_id INT(11)
actor_id INT(11)
phone_number VARCHAR(10)


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

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

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,099 posts
  • LocationLondon

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.grocerycr...tions/set_model and http://www.grocerycr...ample-included/
Posted Image

#3 westerhold

westerhold

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 05 April 2012 - 03: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 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
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;
}
}
?>


#4 Juan Carlos

Juan Carlos

    Newbie

  • Members
  • Pip
  • 5 posts
  • LocationLa Oroya

Posted 02 December 2014 - 05: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
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;
}
}
?>

 

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?






Also tagged with one or more of these keywords: set_relation_n_n

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users