0

I'm having a problem adding more than one player to a team. When I try to insert more than one player it only updates the first inserted entry which belongs to the team.

But let's start at the beginning: When I call find on a team with several players (added directly in the db) I get the following:

Array
(
    [Team] => Array
        (
            [id] => 3
            ...
        )

    [Player] => Array
      (
        [0] => Array
           (
              [id] => 15
              ....
           )
        [0] => Array
          (
              [id] => 21
              ....
          )
        [0] => Array
          (
            [id] => 23
            ....
          )
          ....
    )
)

So I thought I could save it just in that format in to the db. But like I said before the first entry is inserted and then updated with the other id's and I have no idea why. Shouldn't it be just like always that you could save the same structure of data like you get out of the find?

Here is my HABTM relation in the Team Model:

public $hasAndBelongsToMany = array(
    'EmployeesSecondary' => array(
        'className' => 'Player',
        'joinTable' => 'employees_teams',
        'foreign_key' => 'team_id',
        'associationForeignKey' => 'bv',
        'unique' => 'keepExisting' 
    ) 
);

I have already tried using saveAll() (only have on saveAll-call), changed the unique behaviour. I even inserted some test data into the habtm-table and tried saving what I got out from find('all') directly back into the database but it only updates every entry after the first is inserted.

Any idea would be appreciated.

MrSmith
  • 370
  • 4
  • 22
  • You should share the **exact** array you are trying to save, which makes troubleshooting your problem easier. – AgRizzo May 22 '15 at 11:40
  • What would the exact array tell what this one won't? Fact is, that the first habtm-entry is saved and is the updated with the data of the following. Since they all have the same columns it shouldn't make any difference. I changed the id's (forgot to do that after copy + paste) – MrSmith May 22 '15 at 14:13
  • The sharing an exact array has to do with attention to detail and what is correct vs. incorrect. Since I, nor anyone else can read your code, I wanted as much detail as possible to help troubleshoot your problem. You corrected the second biggest flaw in your data that I saw which was the same id. IMO: the other flaw is your "Player" array has the same key (I.e., 0) repeated over and over. My guess is your array is not formatted correctly. But I don't know if this is a poor copy+paste job or if you are trying to save that exact array. – AgRizzo May 25 '15 at 20:16
  • Problem is that I do add habtm-relations directly in the database, get them all via `find()` and try to save them back into the relationtable via `saveAll()` but even this way only the first entry is inserted and later on updated. So when I put 3 player-relations in the habtm-table I still get 1 after trying to save them again. That is what's bothering me. – MrSmith May 26 '15 at 07:55

0 Answers0