CakePHP在保存HABTM连接模型时忽略beforeSave方法 [英] CakePHP ignores beforeSave methods when saving HABTM join model

查看:336
本文介绍了CakePHP在保存HABTM连接模型时忽略beforeSave方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有三个模型:

教练:var $ hasAndBelongsToMany = array(Tour)

Coach: var $hasAndBelongsToMany = array("Tour")

Tour:var $ hasAndBelongsToMany = array(Coach)

Tour: var $hasAndBelongsToMany = array("Coach")

CoachesTour:var $ belongsTo = array(Tour,Coach)

CoachesTour: var $belongsTo = array("Tour", "Coach")

Coach和Tour之间有一个HABTM关联,它应该使用CoachesTour作为连接模型。

There is an HABTM association between Coach and Tour, and it should use the CoachesTour as the join model.

。当修改一个Tour,如果我添加一个新的CoachesTour到它,CoachesTour的beforeSave方法不被调用。看起来好像连接模型的记录被插入为SQL语句,而不是使用连接模型。

I'm using scaffold. When modifying a Tour, if I add a new CoachesTour to it, the beforeSave method of CoachesTour isn't called. It seems as if the records of the join model are being inserted as SQL statements, instead of using the join model.

我在这里缺少了什么?

感谢,

推荐答案

如果您使用saveAll来保存数据,beforeSave回调不调用。
您必须覆盖saveAll函数。

If you use saveAll for save your data, the beforeSave callback will not call. you must overwrite saveAll function.

例如在您的模型中:

public function saveAll($data, $options = array()) {
            /*
            your code you want execute before saving...
            */
    parent::saveAll($data, $options);
}

你必须知道对于其他保存方法(saveMany,saveAssociated,save)beforeSave回调在保存前触发。但对于saveAll它不触发,如果你想保存之前执行一些代码,你可以覆盖它在你的模型。

you must know that for other saving methods(saveMany, saveAssociated,save) beforeSave callback is trigger before saving. but for saveAll it's not trigger and you could overwrite it in your model if you want execute some code for before saving.

这篇关于CakePHP在保存HABTM连接模型时忽略beforeSave方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆