⚠ 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

set_relation get info from a third table



Amgarp
  • profile picture
  • Member

Posted 27 August 2012 - 15:30 PM

Hi,

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!

Konstantin Loshchinin
  • profile picture
  • Member

Posted 06 September 2012 - 10:17 AM

I'm very interested in this as well.
Please, give some tips on this.

victor
  • profile picture
  • Member

Posted 06 September 2012 - 17:28 PM

how many rows will be? which template you use?

victor
  • profile picture
  • Member

Posted 06 September 2012 - 17:55 PM

1)I think you need to make additional requests for each row
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.

Amgarp
  • profile picture
  • Member

Posted 06 September 2012 - 18:42 PM

Hi Victor,

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

victor
  • profile picture
  • Member

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?

Amgarp
  • profile picture
  • Member

Posted 06 September 2012 - 19:46 PM

[b]Works like a charm![/b]

Thank you for your very quick en detailed reply!

victor
  • profile picture
  • Member

Posted 06 September 2012 - 19:48 PM

I'm glad that helped you :)

dinhopereira
  • profile picture
  • Member

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?