cakephp two foreign keys in same table
I have a tournament website with CakePHP, where i need开发者_开发知识库s 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.
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.
精彩评论