result join relation
- Single Page
Posted 17 November 2012 - 02:55 AM
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
Posted 17 November 2012 - 07:06 AM
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.
Posted 17 November 2012 - 10:55 AM
$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.
Posted 17 November 2012 - 18:00 PM
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?
Posted 18 November 2012 - 22:51 PM
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.
Posted 25 February 2013 - 16:15 PM