set_relation get info from a third table
- Single Page
Posted 27 August 2012 - 15:30 PM
I have a situation that I can't solve. Let me try to explain by the following example:
I have three tables:[list]
[*]users
[list]
[*]id
[*]surname
[*]firstname
[*]etc.
[/list][*]projects
[list]
[*]id
[*]title
[*]projectleader_user_id (foreign key to users.id)
[/list][*]actions
[list]
[*]id
[*]project_id (foreign key to projects.id)
[*]date
[*]description
[*]etc.
[/list]
[/list]
Now I want to make an [i]actions [/i]grid with some actions fields (no problem), project title (from the projects table, no problem with set_relation) and the [i]name[/i] of the projectleader (from the users table).
Is that possible?
Any help is appreciated. Thanks!
Posted 06 September 2012 - 10:17 AM
Please, give some tips on this.
Posted 06 September 2012 - 17:28 PM
Posted 06 September 2012 - 17:55 PM
2) or create your own model.
The first version of a simple, but more load on the server.
if you are interested in the first option, I'll show you.
Posted 06 September 2012 - 18:42 PM
Thanks for your reply. I use flexigrid and (I think) I'm interested in the first option, so please show me. The load on the server is no issue (yet...).
Thanks already,
Koos
Posted 06 September 2012 - 18:47 PM
function action()
{
$crud = new grocery_CRUD();
$crud->set_table('test_actions');
$crud->set_subject('action');
$crud->columns('id','project_id','date','description','user_name');
$crud->set_relation('project_id','test_projects','title');
$crud->display_as('project_id','Project name');
$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;
}
result:
[attachment=273:jpg_2.jpg]
helped you?
Posted 06 September 2012 - 19:46 PM
Thank you for your very quick en detailed reply!
Posted 06 September 2012 - 19:48 PM
Posted 27 September 2017 - 03:29 AM
GREAT !!!!
Sorry for getting back on such an old topic, but that was very useful today.
I would like to display the name also during insertion in the select.
It's possible?