for argument's sake. Since the names are unique (it's actually more complex than this) I check for that name, if it exists I set the "id" field in the data to the id of the record in the table (because I want to update not to insert).
Needless to say, this doesn't work. There are two things that go wrong:
1) I have to cycle through each item and use save() because saveAll() is only taking the first item in the array.
2) No matter how I try and set the ID I can't get it to do anything except insert. How do I make it update?
Not sure I fully understand your issue - can you post your code (the relevant bits of the view and controller in particular)? Also if you are trying to enforce uniqueness, you can use standard model validation for this. There is predefined rule for enforcing unique values.
The first rule of Tautology Club is the first rule of Tautology Club.
Uniqueness is enforced in the tables (so thank you for that, I will implement this) but that is not so much my problem as how to trigger update instead of save.
Basically I've been doing this:
What I want is that if this particular "Host" exists (i.e. a record with the same project_id and institution_id) then that record should be updated (because there is some information which could have changed and is in $this->data["Host"]).
I had previously tried using $this->data["Host"]["id"] instead of $this->id but it didn't work either. It always tries to insert rather than update. Therefore I get the following SQL error:
Code:
SQL Error: 1062: Duplicate entry '1' for key 'PRIMARY'
If I use $this->data["Host"]["id"] and
Code:
SQL Error: 1062: Duplicate entry '1-15' for key 'unique_host'
Are you trying to run this by calling saveAll from a related model? If so, you will probably find that cake is internally storing the data array at the moment you call saveAll, so even though you modify $model->data in beforeSave, these changes are effectively ignored, meaning the FK is never set. You can test for this by using debug($this->data) and comparing it to the SQL log. If that is the issue you can try moving this logic to the controller (as dirty as that solution makes me feel), or else to the beforeSave of the main model (which IIRC is not affected).
The first rule of Tautology Club is the first rule of Tautology Club.
That's what I meant by saving via a related model.
The weird thing is that it sets the ID fine (because I get the primary key error) but it uses insert and not update for some bizarre reason.
Yes, even though it appears to set it be set, the moment you call saveAll, cake internally saves the data state right then, so anything you do in beforeSave is ignored. I've never seen this documented anywhere, I had to dig through the source code to find it out - you're not the only one who's lost hours to this "feature"!
Maybe I should just make a "clean up data" function for each model and save the data that comes out of that?
That's what I end up doing usually. It's a bit messy and I don't really like it, but it does the job. The other option is just not to use saveAll - it's a bit of a nasty function really.
The first rule of Tautology Club is the first rule of Tautology Club.
Bookmarks