I’m new to CakePHP and hope someone could help me with this one. I can’t solve it on my own.
I have a table ‘games’, with the following columns:
- id
- date
And a second table ‘reports’, with the following columns:
- id
- report
- game_id (foreign key)
Game hasOne report and report belongsTo game.
I’m trying to build a form that adds a report to an existing game. Whenever I add a new report, it creates a duplicate record. I have read that if Cake doesn’t know the id, this will happen. And when I debug, the id-field is empty.
This is my aim: If there isn’t a report for a game yet, an add-link will appear and if a report exists for a specific game, there’s update link. Or there’s respectively one link to a form that adds or edits report depending on if there is/isn’t one. How this can be done?
I have read that in beforeSave() -function, you could specify when create and when edit a record. I don’t have an idea how this can be done.
Currently my only solution is to validate the form and set game_id isUnique and to inform to the user that there’s already a report with this game_id.
Big thank you to anyone, who could help me with this!
You could just check beforehand.