Cakephp two foreign keys in one table

I have a tournament site with CakePHP where I need to manage the following condition:

There will be a competition between two rivals, where I need to manage the arrangement of matches and match schedules, and here the opponent and the opponent will be the same from the foreign key of the competitors table, which means that the same user will be against each other than in this case, like I can save two identical fields (competitor_id) in the table of the schedule of matches, and the administrator can also manage orders of competitors, if any competitor is not available, etc.

+4
source share
1 answer

Your question is a little confused until the end, are you just trying to create 2 relationships from a planned event and users (an event with a competitor and an adversary)? If so, this can be achieved by expanding your relationships in your schedule model with foreign keys.

Instead of saying:

var $hasMany = array('Competitor'); 

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

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

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

+6
source

Source: https://habr.com/ru/post/1343136/


All Articles