LoginSignup
2
1

More than 5 years have passed since last update.

CakePHP3 アソシエーションされたモデルのバリデーション

Last updated at Posted at 2016-12-14

要約

patchEntityする際にassciatedにアソシエーション先モデル名を記述するだけでいけました。

詳細

久しぶりに引っかかったので備忘録として…

アソシエーションされたモデルのバリデーションを行うには以下のように記述する。

if ($this->request->is(['patch', 'post', 'put'])) {
  $user = $this->Users->patchEntity($user, $this->request->data, ['associated' => 'Persons']);
  if ($this->Users->save($user)) {
    $this->Flash->success('ユーザ情報を更新しました。');
    return $this->redirect(['action' => 'mypage']);
  } else {
    $this->Flash->error(__('ユーザ情報の更新に失敗しました。'));
  }
}

参考URL

2
1
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
2
1