CakePHP 3 - beforeSave回调无法编辑

yBr*_*sky 3 cakephp edit callback cakephp-3.0

我有一个beforeSave-callback,每当我创建一个新实体时都会被调用.但是当我编辑时,它根本就没有被调用.在文档中找不到任何可能有用的内容.

这是我的编辑功能:

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid article'));
    }

    $article = $this->Articles->get($id);
    if ($this->request->is(['post', 'put'])) {
        $this->Articles->patchEntity($article, $this->request->data);
        if ($this->Articles->save($article)) {
            $this->Flash->success(__('Your article has been updated.'));
            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('Unable to update your article.'));
    }

    $this->set('article', $article);
} 
Run Code Online (Sandbox Code Playgroud)

eM.*_*eM. 14

beforeSave仅当您发布/编辑的数据被修改时,才会触发该功能.

//triggers only if an entity has been modified
public function beforeSave(Event $event, Entity $entity)
{
    if($entity->isNew()) {       
        //on create
    } else {
        //on update
    }
}
Run Code Online (Sandbox Code Playgroud)