If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] CakePHP saving makes me want to cry

And this is probably because I am misunderstanding horribly, but please (think of the children?)

Anyway, here we go.

I have a page which edits associated models links to the current model (has many actually). I pass in a data array into save all like so:

Code:

array => 0 => array("name"=>"One"), 1=> array("name"=>"Two");

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.

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:

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).

First time around I tried doing a saveAll from a related model. Next I tried doing it from

PHP Code:

$this->Model1->Model2->saveAll($data["Model1"]["Model2"])

and that didn't work either...

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.