Issue
How does CakePHP deal with tables that don't have an id
column?
HABTM is a "has and belongs to many" table, which means a many to many database relationship
I'm trying to save some relationship data, but Cake wants to "SELECT LAST_INSERT_ID()", however the table that it's trying to save to does not have the id column so the id is for a different table.
Specifically, I've got tables for "Games" and "Players", and a relationship table called "game_players". game_players has the fields game_id and player_id, and it's on this table that I can't save the relationship data.
To clarify: it's the game_player table that's causing the problem. This table does not have, nor should it have, an id field. It's only storing relationships between player and game, and the primary key is (game_id, player_id)
How does CakePHP handle this situation/relationship?
Solution
Does Game HABTM Player, or vice versa? If so, you should not have an ID field on your join table games_players. And you should be able to save data to it by calling save on Game with a data array like
$this->Game->save(array(
'Player'=>array(
'Player'=>array(
1,4,5,9 // Player IDs
)
)
));
In this case, Cake will not try and get the last insert id.
If however, Game does not HABTM Player, and you are calling save on your model GamePlayer directly, it should have an ID field to fit with Cake conventions.
Answered By - neilcrookes
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.