0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CakePHP:afterSaveの使い方

Posted at

CakePHP5でafterSaveの使い方に詰まったので、備忘録として。
公式サイト↓
https://book.cakephp.org/5/ja/orm/table-objects.html

use ArrayObject;//呼び出しを忘れずに
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;

public function afterSave(EventInterface $event, EntityInterface $entity, ArrayObject $options){
    if($entity->isNew){//もし新規登録だったら。
        $entity->file_name = $entity->id . '_example';//例えば、file_nameを"id"_exampleにしたい
        $this->save($entity);//最後にSaveすることを忘れずに!
    }
    
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?