ArticlesController.phpで以下の様に記事を保存する際
use Cake\ORM\TableRegistry;
$articlesTable = TableRegistry::get('Articles');
$article = $articlesTable->newEntity();
$article->title = 'タイトル(新規)';
$article->body = '本文';
if ($articlesTable->save($article)) {
return $this->redirect(['action' => 'index']);
}
保存前に特定の処理を実行したい時があります。
そういうときはArticlesTable.phpにて、以下の様なメソッドを実装します。
public function beforeSave($event, $entity, $options)
{
if ($entity->isNew()) {
// 新規の記事の場合、本文の書き換え
$entity->set('status', "New");
}
return true;
}


