由于我的应用程序是如何构建的,我需要为beforeSaveAssociated
和创建事件处理程序afterSaveAssociated
。为此,我更新了 AppModel.php 以包含以下相关代码:
public function saveAssociated(array $data = null, array $options = array()) {
$this->after_save_options = NULL;
$event = new CakeEvent('Model.beforeSaveAssociated', $this, array(&$data, &$options));
$this->after_save_options = NULL;
$this->getEventManager()->dispatch($event);
if (parent::saveAssociated($data, $options)) {
if (is_array($this->after_save_options)) {
$curData = $this->data;
$this->data = $this->_tempData;
$event = new CakeEvent('Model.afterSaveAssociated', $this, $this->after_save_options);
$this->after_save_options = NULL;
$this->getEventManager()->dispatch($event);
$this->data = $curData;
}
if ($this->_tempData) {
$this->_tempData = FALSE;
}
return TRUE;
}
return FALSE;
}
public function implementedEvents() {
return array_merge(parent::implementedEvents(), array(
'Model.beforeSaveAssociated' => array(
'callable' => 'beforeSaveAssociated',
'passParams' => TRUE,
),
'Model.afterSaveAssociated' => array(
'callable' => 'afterSaveAssociated',
'passParams' => TRUE,
),
));
}
尽管这对于beforeSaveAssociated
模型类中定义的任何东西都适用,但每当我在行为中定义它时,它都不会被触发。如果我saveAssociated
在上面更新以触发Model.beforeSave
(内置事件),它确实有效,据我所知,这不是行为未正确附加的问题。
任何帮助是极大的赞赏,