CakePHP ignores beforeSave methods when saving HABTM join model

若如初见. 提交于 2019-12-11 11:35:21

问题


So I have three models:

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

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

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

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

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.

Am I missing something here?

Thanks,


回答1:


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

for example in your model:

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

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.



来源:https://stackoverflow.com/questions/11767555/cakephp-ignores-beforesave-methods-when-saving-habtm-join-model

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