4

I have a tournament website with CakePHP, where i needs to manage a condition like below details:

There will be a fight competition between two competitor's where I needs to manage a match division and match schedules, and here Competitor and Opponent will be the same from the competitor's table's foreign key, means same user will be opposed to each other, than in this case, how can i save two same fields (competitor_id) into the match schedule's table for and admin can also manage, competitor's orders, if some competitor is not available etc.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104

1 Answers1

6

Your question gets a little confusing towards the end, are you trying to simply create 2 relationships from a scheduled event and users (event with competitor and opponent)? If so, this can be achieved by expanding your relationships in your schedule model with foreign keys.

Instead of say:

var $hasMany = array('Competitor');

You can expand and set the foreign key and table name:

var $hasMany = array(
 'Competitor' => array(
        'className' => 'Competitor',
        'foreignKey' => 'competitor_id'
    ),
 'Opponent' => array(
        'className' => 'Competitor',
        'foreignKey' => 'opponent_id'
    )
);

This will setup 2 relationships to the same model, and you can save them separately. Further reading.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • hey, sorry about the confusing words, but i mean to say, competitor and opponent are not separate things, both will be the competitors only, but i want to manage them like, one as competitor and one as opponent, hope this clear you more.. – Aditya P Bhatt Mar 10 '11 at 10:11
  • Cool, then my answer should be fine :) Both competitor and opponent will come from the same table but with different foreign keys. – Dunhamzzz Mar 10 '11 at 11:14
  • 1
    This is called aliases :) should help you if you want to search some more. – dogmatic69 Mar 10 '11 at 19:40