我的插件行为是读取数据数组,执行它应该做的所有事情,但是我的控制器中的数组没有任何反应,它保持不变。
控制器代码:
if($this->request->is('post') || $this->request->is('put')) {
$this->request->data['Task']['id'] = $task['Task']['id'];
if($this->Task->saveAssociated($this->request->data, array('deep' => true))) {
debug($this->request->data);exit;
$this->redirect(array('action' => 'view', $slug));
}
$this->setFlash(__('TASK_EDIT_FAILED'));
$this->redirect(array('action' => 'view', $slug));
}
行为代码:
public function beforeSave (Model $Model, $options = array()) {
App::import('Model', 'Upload.Attachment');
$this->Attachment = new Attachment();
if(isset($Model->data[$Model->alias]['Attachment'])) {
foreach($Model->data[$Model->alias]['Attachment'] as $key => $attachment) {
$data['Attachment'] = $attachment;
$data['Options'] = array(
'model' => $Model->alias,
'root' => WWW_ROOT . 'files',
);
$this->Attachment->create();
$this->Attachment->save($data);
$Model->data['Attachment']['Attachment'][$key] = $this->Attachment->getInsertID();
unset($Model->data[$Model->alias]['Attachment'][$key]);
}
}
return true;
}
修改
在保存之后,任务控制器中的$this->request->data
不会发生变化,而任务模型中的$this->data
已更改(我在任务 - > afterSave()函数中调试了$this->data
)。
答案 0 :(得分:0)
如果您在任务模型中编写了行为,则无需执行此操作。所以删除它。
$actsAs = array('Upload.Attachment');
也不是这个
if($this->Task->saveAssociated($this->request->data, array('deep' => true))) {
使用此
if($this->Task->save($this->request->data, false)) {
如果你使用的是cakephp 2.x而不是
App::import('Model', 'Upload.Attachment');
使用它来加载模型中的其他模型。
App::uses('Upload', 'Model');