3

I'm creating a tournament-platform with CakePHP. Currently I've created the following tables, models and controller which work perfectly: Tournaments, Users, and Teams.

I've created a "Tournamentuser" table, controller & model as well, where the tournament_id, user_id and team_id is stored.

Then when using View.ctp under Tournaments, it should display the User.Username & Team.Username in relation to the Tournamentuser. As of now, I have only managed to retrieve the id's from the table via. a foreach(.. as ..). I can see in the CakePHP query that it retrieves the data, I just don't know how to print it.

tshepang
  • 12,111
  • 21
  • 91
  • 136
2mas
  • 127
  • 2
  • 14

2 Answers2

1

I suppose you already know this but in CakePHP the conventions are:

  • models must have singular name: user/car/image
  • controllers must have plural name: users/cars/images
  • tables must have plural name: users/cars/images

If for example we need a relation: tournament-user we must follow the conventions(in CakePHP 2+):

  • controller: TournamentsUsersController.php
  • model: TournamentsUser.php
  • table: tournaments_users

If all the above conventions are followed before you can have your relation, tournaments-users you must define the relation between the two tables.

Here you can find more details on how to do this: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

If everything is done correctly until this point, you can now query the TournamentsUser Model.

Assuming you are in TournamentsUsersController.php controller:

$data = $this->TournamentsUser->find('all');

You can now see what $data contains using:

debug($data);

For printing the data you can use either a for/foreach loops:

$size = count($data);
for($i=0; $i<$size; ++i) {
   debug($data[$i]);
}
Catalin MUNTEANU
  • 5,618
  • 2
  • 35
  • 43
  • Hi Catalin, I've managed to set up the relation fine, but when getting info from 2nd degree of "join" Tournament > TournamentUser > User - I can't seem to set this up. I can get to TournamentUser (where the user_id) is stored - but getting the Username is more difficult. – 2mas Sep 07 '12 at 19:58
  • Yeah correct, but when inserting $blabla['User']['username'] i get the "Undefined index" for User – 2mas Sep 07 '12 at 20:09
  • You could just post here your code or at least the part where you are having trouble. You said that you can't fetch the username and now you are having trouble inserting the username. Which one is it? What are you trying do to? – Catalin MUNTEANU Sep 07 '12 at 20:15
  • (http://pastebin.com/KK90hB0E)

    , ,

    - It's the ['User']['username'] that is now working.
    – 2mas Sep 07 '12 at 20:26
  • Inside the foreach loop use debug(); to see if the $tournamentuser actually contains ['User'] array. Also you should never have ['Tournamentuser']. I have no idea how you got here but if you would follow CakePHP conventions you would have your data under ['TournamentsUser']. – Catalin MUNTEANU Sep 07 '12 at 20:34
  • The table name is 'Tournamentuser', so the name should work. Should I name it otherwise? I've tried the debug inside the foreach loop, and the ['User'] doesn't display, so I guess it must be something with my TournamentsController. Here's my controller: http://pastebin.com/Y7wKTnk6 – 2mas Sep 07 '12 at 22:34
  • Inserting recursive=>2 in my view()->User-query doesn't seem to do the trick. I've might have done it wrong: `$this->set('users', $User->find('all', array('recursive' => 2)));` Should i set this up other places aswell? – 2mas Sep 08 '12 at 09:11
  • $query = $this->Model->find('all', array('recursive' => 2)); – Catalin MUNTEANU Sep 08 '12 at 12:01
  • Hmm, I'm not quite sure where to insert it. Is it inserted correctly here: http://pastebin.com/13zpwGW3 ? – 2mas Sep 08 '12 at 12:27
0

If you're getting the data, but just need to access the values, you can use normal PHP array functionality:

echo $myData['User']['username'];
echo $myData['Team']['username'];

Or, if you have more than one:

foreach($myData as $data) {
    echo $data['User']['username'];
    echo $data['Team']['username'];
}

Obviously this will need to be tweaked per your code, but it should help you get the idea.

Dave
  • 28,833
  • 23
  • 113
  • 183
  • Hi Dave, I'm getting a "Undefined index" when using 'User' or 'Team'.The data I'm getting is from Tournamentuser - not the User-table - and it's only the ID's. – 2mas Sep 07 '12 at 14:04
  • user1654844 - then it's probably a good idea to debug your data, and just follow the guidelines above to access the data based on what data you're actually getting. – Dave Sep 07 '12 at 14:11
  • As Dave mentioned, I'm already using the foreach(....), but when using ['User'] or ['Team'], I get the Undefined index. – 2mas Sep 07 '12 at 19:50