i try to create football table app. i have 3 tables - teams(id, name)
, matches(id, home_team_id, away_team_id, round_id)
and rounds(id, round_num)
. home_team_id
and away_team_id
are foreign keys, reference to teams
table. and round_id
is f.k. references to rounds
table. for example i have 4 teams - juventus
, milan
, chelsea
, arsenal
. so if i want to create match - first i create round. all i need here is just insert number of round(for example 4th round). then i go and add matches in this round - choose home team(for example juventus
) and then choose away team(for example milan
) and then submit. the problem is, next time when i will create match, i shouldn't be able to create match that already been played. if juventus
played home against milan
user shouldn't be able to create this match again. so i need home_team_id
and away_team_id
to be unique together, and only together(not separately)! how should i do this? i tried to validate it like
[['home_team_id', 'away_team_id'],
'unique', 'targetClass' => Match::className(),
'targetAttribute' => ['home_team_id', 'away_team_id']],
but instead of prevent storing data and show error message,it override existing match