在 CakePHP 1.3 中,是否有在 `saveAll()` 之后使用的回调?

发布于 2024-10-15 02:38:42 字数 414 浏览 2 评论 0原文

使用 CakePHP 1.3,是否有一个在模型上的 saveAll() 之后触发的回调,或者实现此类行为的方法?

也许 afterSave() 已经做到了这一点?

具体来说,我想运行几个特定的​​方法,但仅在保存相关项目后,并且仅当父项目是新保存的实例时才运行。

$created 参数这样传递给 afterSave() 的东西,显然看起来很完美,但我至少 90% 确定 afterSave() > 在初始保存后对模型进行调用 - 我理解这必须在保存相关模型之前发生(以便它们可以在 FK 字段中放入一些内容)。

您对实现这种行为有何建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-10-22 02:38:42

CakePHP 中没有内置 Model::saveAll() 回调,但我相信您可以重写模型上的该方法来创建您自己的方法,如下所示:

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

function afterSaveAll() { 
    // Callback code. 
}

我目前不确定如何然而,生成类似于 Model::afterSave()$created 变量行为。

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.

高速公鹿 2024-10-22 02:38:42

afterSave() 就像 save() ...

它为每个模型调用,saveall 只是带有 save() 的 foreach,因此 afterSave 将在调用最终 save() 的每个模型中调用

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

墨落成白 2024-10-22 02:38:42

你不能做这样的事情吗:

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

也许你可以知道这是一个创建的项目,因为你不会传递 id。我不知道,因为你还没有发布任何代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文