0

I have a problem with my HABTM table.

This is the model structure: Team <- EmployeesTeam -> EmployeesAdditionalData

'EmployeesSecondary' => array(
    'className' => 'EmployeesAdditionalData',
    'joinTable' => 'employees_teams',
    'foreign_key' => 'team_id',
    'associationForeignKey' => 'bv'
) 

(bv is some sort of an Employee ID)

Everytime I try to save data in the HABTM Table with $this->Team->save() as well as $this->Team->EmployeesTeam->save() it saves the first entry perfectly, but then assumes, that the team_id would be the primary_key (even when I set the bv as PK or erase the whole primary index).

So, when I try to assign another Employee to the Team it updates the first entry and doesn't insert a new one.

INSERT INTO `employees_database`.`employees_teams` (`bv`, `team_id`) VALUES (3781, 3);

UPDATE `employees_database`.`employees_teams` SET `bv` = 3345, `team_id` = 3 WHERE `employees_database`.`employees_teams`.`team_id` = '3'

Is there something I'm doing wrong?

Tony
  • 11
  • 5
  • What are the different indexes which are on the table employees_teams? It sounds like team_id is either a primary or unique key – gabe3886 May 27 '15 at 08:06
  • First I had two primary keys: bv and team_id like you do in m:n tables. While debugging I set only bv as only primary key and also deleted the primary index. The result was always the same. They're also both not set unique. – Tony May 27 '15 at 08:34
  • I'd remove the primary keys from the table, and then have a combination of the bv and team_id as a unique key. If you really need them to be individually indexed, then set those fields to be a normal index rather than a primary or unique key – gabe3886 May 27 '15 at 08:38
  • Doesn't work either. I just checked the primary key of the virtual model with `pr($this->Team->EmployeesTeam->primaryKey);` It really is the team_id. I never set a primary_key. Why did CakePHP set a primary key? – Tony May 27 '15 at 09:39

0 Answers0