⚠ 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

Show more than one result - relation n



Julio

Julio
  • profile picture
  • Member

Posted 24 May 2013 - 01:40 AM

Hi,

 

Is there any way to show more than one field result with set_relation?

 

Ex.

table1

code - 1

description - A

description2 - B

$crud->set_relation('code','table','description.description2'); 

combo result -> A B

 

Thanks


davidoster

davidoster
  • profile picture
  • Member

Posted 24 May 2013 - 06:04 AM

From the page http://www.grocerycrud.com/documentation/options_functions/set_relation

 

,

 

"You can have as many fields you like to call from the other table and the syntax is really simple. Just at the 3rd field you will have the symbol { and } . So it will be for example:

 
$crud->set_relation('user_id','users','{username} - {last_name} {first_name}');

"


Julio

Julio
  • profile picture
  • Member

Posted 24 May 2013 - 13:39 PM

Thanks David for your reply.

 

Just one more question...

Could it be possible to make table join?

 

I meam... using your example make join between users and groups tables and at the 3rd field the return would be {username} {groupname}, in other words username from users table and groupname from groups table.


Julio

Julio
  • profile picture
  • Member

Posted 26 May 2013 - 22:35 PM

I got 3 tables relation and i need to show at dropdown list the description field from 2 of this 3 tables. I've tried to get this using a callback_field, but with no success. With callback_column i got the expect result.

 

The dropdown list should be populated with 2 description fields: {table1.description} {table2.description} at add/edit page, but the list still show only 1 field {table1.description}.

 

My control:

	function preparar_escalas() {
		$crud = new grocery_CRUD();
		
	    $crud->set_theme('datatables');
	    $crud->set_table('e050pge');
 		$crud->display_as('CODLOC','Locais/Veículos');		
		$crud->set_relation('CODLOC','e010loc','DESLOC',array('SITLOC' => 'A'));		

		$crud->set_relation_n_n('Regionais', 'e050pxr', 'e015reg', 'CODPGE', 'CODREG', 'DESREG', 'CODPRI');

		$crud->display_as('CODJOR','Jornada');
		$crud->callback_column($this->unique_field_name('CODJOR'),array($this,'_callback_e032esc_desesc'));
		$crud->callback_field($this->unique_field_name('CODJOR'),array($this,'_callback_e032esc_desesc'));		
		$crud->set_relation('CODJOR','e038jor','DESJOR');

		$crud->set_relation_n_n('Pessoas', 'e050pxp', 'e030pes', 'CODPGE', 'CODPES', 'DESNOM', 'CODPRI');
		$crud->display_as('DATINI','Data Inicial');
		$crud->display_as('DATFIM','Data Final');		
		$crud->display_as('SITPGE','Situação');
		$crud->columns('CODLOC', 'Regionais','CODJOR','Pessoas','DATINI','DATFIM','SITPGE');
		$crud->fields('CODLOC', 'Regionais','CODJOR','Pessoas','DATINI','DATFIM'); 		
	    $output = $crud->render();
		$this->template($output);	
	}

	function _callback_e032esc_desesc($value, $row){
		return $this->main_model->pegar_descricao($row);		
	}

	function unique_field_name($field_name) {
	    return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
    }

My Model:

	function pegar_descricao($row){
		
			$this->db->select('e038jor.codjor, e038jor.desjor, e038jor.codesc, e032esc.desesc');
			$this->db->from('e038jor');
			$this->db->join('e032esc','e038jor.codesc = e032esc.codesc');
			$this->db->where('e038jor.codjor',$row->CODJOR);
			$dados = $this->db->get()->result();
			return $dados[0]->desjor . ' - ' . $dados[0]->desesc;	
	}

Is there any way to show the results as I expected?

Or am I doing anything wrong?

 

Thanks.


davidoster

davidoster
  • profile picture
  • Member

Posted 29 May 2013 - 04:41 AM

Check this sample code to get an idea.

// controller
$this->load->model('itemmodel');
// multiple activities
$activities = $this->itemmodel->get_activities("activities");
foreach ($activities->result() as $row)
{
$myarray[$row->id] .= $row->description;
}
$this->grocery_crud->field_type('interest_for','multiselect',$myarray);

//model
public function get_activities($table)
{
$this->db->select('id, year, description');
$this->db->order_by('id desc');
return $this->db->get($table);
}

If you think of it, the above is like having a set_relation but defined exactly the way I need it to be.

The model returns the whole table result and in the controller I decide on the left side of the array to match the result (just one field here).

For more complex relations, such as fields from joined tables etc, I would just put on the right side of the array the fields I needed concatenated.

 

I hope this helps you. It's not very different from yours but it's a very simple approach were you make your own set_relations!


Julio

Julio
  • profile picture
  • Member

Posted 30 May 2013 - 16:45 PM

David, perfect.

 

Thanks for your help. Solved my problem.