⚠ 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

Relation based on ID



DerekJR321

DerekJR321
  • profile picture
  • Member

Posted 09 January 2015 - 18:50 PM

Hey all,

 

This is going to sound like an amateur question but here it goes.

 

I have two tables... "Teams" and "Players".

 

Teams has "id" and "team_name". The "Players" table has a field called "team_id" which relates to "teams.id". Follow so far?

 

So basically.. what I want to do is, instead of showing the team id, I want to show the team_name. (example: teams.id=1, teams.team_name="Mets", players.team_id=1)

 

I can't for the life of me figure out how to do this with Grocery Crud. I know how to do it with regular mySQL code. Here is what my controller looks like so far.

 

public function manage_players() {
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('players');
        $crud->display_as('number', 'Player Number');
        $crud->display_as('name', 'Player Name');
        $crud->display_as('position', 'Position');
        $crud->display_as('height', 'Height');
        $crud->display_as('weight', 'Weight');
        $crud->display_as('dob', 'Birthday');
        $crud->display_as('shoots', 'Shoots');
        $crud->display_as('gp', 'Games Played');
        $crud->display_as('goals', 'Goals');
        $crud->display_as('assists', 'Assists');
        $crud->display_as('points', 'Points');
        $crud->display_as('image', 'Player Photo');
        $crud->set_field_upload('image', 'assets/images/players');
        $crud->set_relation('what goes here');
        $output = $crud->render();

        $this->_players_output($output);
    }

 

PS- This is for a hockey league project I am working on for my kid.

 

 

Thanks for any help...

 

Derek

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 10 January 2015 - 04:47 AM

kid - its simple - just use set_relation('team_id', 'teams', 'team_name') ... and magically.. u have the team name:)


DerekJR321

DerekJR321
  • profile picture
  • Member

Posted 11 January 2015 - 15:11 PM

Thanks. That worked. I guess I figured it had to be more complicated than that :)

 

Derek