⚠ 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

result join relation



andrea
  • profile picture
  • Member

Posted 17 November 2012 - 02:55 AM

I want to know if I can get from a join of two tables all the fields of the tables concerned in the relation.
It seems to me that: 'set_relation_n_n' and 'set_relation' add to first table fields only ONE field, this field has a concatenation of the fields of the second table but this solution does not allow to correctly represent the relation result.
thanks for help

victor
  • profile picture
  • Member

Posted 17 November 2012 - 07:06 AM

Hi andrea and welcome to the forum.
You can create a new model as solution.

Or you can use this solution :

function action()
{
$crud = new grocery_CRUD();
$crud->set_table('test_actions');
$crud->columns('id','project_id','date','description','user_name');

$crud->set_relation('project_id','test_projects','title');

$crud->callback_column('user_name', array($this, 'user_name'));
$output = $crud->render();

$this->load->view('test', $output);

}

function user_name($value, $row)
{
$this->db->select('* , test_users.id as user_id');
$this->db->join('test_users', 'test_projects.projectleader_user_id = test_users.id', 'left');
$data = $this->db->get_where('test_projects',array('test_projects.id' => $row->project_id))->row();
return $data->firstname.' '.$data->surname;
}



The function "Columns" can create an unlimited number of columns in the list.
In this example was created an extra field [color=#ff0000]user_name [/color](it doesn't exist in a DB).
Then you can use a callback function for this column.

davidoster
  • profile picture
  • Member

Posted 17 November 2012 - 10:55 AM

Why don't you use this formatting that is explained in the documents?
$this->grocery_crud->set_relation('customers_id','customers','{last_name} {first_name} {any_other_fieldname}');
This way gives you absolute control on how many fields and which of them are displayed, not just "ONE" as you mentioned!
Check it here, http://www.grocerycrud.com/documentation/options_functions/set_relation.

andrea
  • profile picture
  • Member

Posted 17 November 2012 - 18:00 PM

Hi Victor thanks for response,
ok I understand your solution, but to fill the extra field user_name [color=#000000]you [/color]run an additional query and this for each field I want to add?

aherus
  • profile picture
  • Member

Posted 18 November 2012 - 22:51 PM

[quote name='David Olive Tree Oster' timestamp='1353149716' post='4302']
Why don't you use this formatting that is explained in the documents?
$this->grocery_crud->set_relation('customers_id','customers','{last_name} {first_name} {any_other_fieldname}');
This way gives you absolute control on how many fields and which of them are displayed, not just "ONE" as you mentioned!
Check it here, http://www.grocerycr...s/set_relation.
[/quote]

Thanks a lot! I wasted my time for trying to use callback_column. Next time I have to practise my english and read documentation three times and another one with google translator. :)

Felipe Matos
  • profile picture
  • Member

Posted 25 February 2013 - 16:15 PM

In my case, is similar. But I dont need to show the rows (events) in the list. I want to add, edit and delete (on cascade) the events (positions in example above) together with the table project.
 
My structure of tables is...
 
Projetct(id, name)
Event (id, name, id_projetct)
Activit (id, name, id_event)
 
How I do this?