In CakePHP 1.3, is there a callback for use after a `saveAll()`?

廉价感情. 提交于 2019-12-11 07:05:52

问题


Using CakePHP 1.3, is there is a callback that is fired after a saveAll() on a model, or a way to implement such a behavior?

Maybe afterSave() already does this?

Specifically, I would like to run a couple particular methods, but only after related items have been saved, and only if the parent item is a newly saved instance.

Something like the $created argument, passed to afterSave(), obviously seems perfect, but I'm at least 90% certain that afterSave() is called on a model after the initial save -- which I understand has to happen before the related models are saved (so that they have something to put in the FK field).

What do you suggest for obtaining this behavior?


回答1:


There isn't a callback for Model::saveAll() built into CakePHP, but I believe you can override that method on the model to create your own, like so:

// In your Model class...
function saveAll($data = null, $options = array()) {
    parent::saveAll($data, $options); 
    $this->afterSaveAll(); // Your new custom callback. 
}

function afterSaveAll() { 
    // Callback code. 
}

I am not currently sure about how to produce a $created variable behavior similar to what Model::afterSave() has, however.




回答2:


afterSave() just like save() ...

its called for each model, saveall is just a foreach with save() so the afterSave will be called in each model that the final save() is called in




回答3:


Can't you just do something like this:

if($this->Recipe->saveAll($this->data)) {
    //Do some stuff and checking for new insert.
    $this->Recipe.doSomeStuff();
    $this->redirect('/recipes');
}

Perhaps you can tell that it's a created item because you won't be passing an id. I don't know because you haven't posted any code.



来源:https://stackoverflow.com/questions/4857073/in-cakephp-1-3-is-there-a-callback-for-use-after-a-saveall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!