在 CakePHP 1.3 中,是否有在 `saveAll()` 之后使用的回调?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CakePHP 中没有内置
Model::saveAll()
回调,但我相信您可以重写模型上的该方法来创建您自己的方法,如下所示:我目前不确定如何然而,生成类似于
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:I am not currently sure about how to produce a
$created
variable behavior similar to whatModel::afterSave()
has, however.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
你不能做这样的事情吗:
也许你可以知道这是一个创建的项目,因为你不会传递 id。我不知道,因为你还没有发布任何代码。
Can't you just do something like this:
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.