CakePHP afterSave Timing
I have a situation where, in a model's afterSave
callback, I'm trying to access data from a distant association (it's a legacy data model with a very wonky association linkage). What I'm finding is that within the callback I can execute a find call on the model, but if I exit right then, the record is never inserted into the database. The lack of a record means that I can't execute a find on the related model using data that was just inserted into the current.
I haven't found any mention of when data is actually committed with respect to when the afterSave
callback is engaged. I'm working with legacy code, but I see no indication that we're specifically engaging transactions, so I'm trying to figure out what my options might be.
Thanks.
UPDATE
The gist of the scenario is this: We're taking event registrations, but folks can be wait listed. A user can register (or be registered) for a given Date
. After a registration is complete, I need to check the wait list for the existence of a record for the registering user (WaitList.user_id
) on the date being registered for (WaitList.date_id
). If such a record exists, it can be deleted because it's become an active registration.
The legacy schema puts me in a place where the registration isn't directly tied to a date so I can't get the Date.id
easily. Instead, Registration->Registrant->Ticket->Date
. Unintuitive, I know, but it is what it is for now. Even better (sarcasm included), we have a view named attendees
that rolls all of this info up and from which I would be able to use the newly created Registration->id
to return Attendee.date_id
. Since the record doesn't exist, it's not available in the view.
Hopefully that provides a lit开发者_如何学JAVAtle more context.
What's the purpose of the find query inside of your afterSave?
Update
Is it at all possible to properly associate the records? Or are we talking about way too much refactoring for it to be worth it? You could move the check to the controller if it's not possible to modify the associations between the records.
Something like (in psuedo code)
if (save->isSuccessful) {
if (onWaitList) {
// delete record
}
}
It's not best practice, but it will get you around your issue.
精彩评论