Php – How does CakePHP handle HABTM tables with / without an ‘id’ field


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?

Best Solution

var $primaryKey = 'example_id'; $primaryKey is what do you looking for.

Just place this on your model class.