问题
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