User モデルが Profile モデルを HasOne で保持しています。Profileモデルの値を更新して保存するときに UsersTable::save() を使いたいんですが patchEntity() を使わないと保存されないのは割り切って使うしかないんでしょうか?
具体的にはこんなコードです。
php
1class User extends Entity { 2 // ユーザの属性とプロフィールを $data の値で更新する 3 public function updateProfile(array $data) 4 { 5 // いろいろUser自身の属性を更新 6 // 住所情報を更新 7 $this->profile->changeAddress($data['address']); 8 // $this->setDirty('profile', true); これを実行すれば保存される 9 } 10}
php
1class Profile extends Entity 2{ 3 // 住所変更メソッド 4 public function changeAddress($newAddress) 5 { 6 // 離島に住んでいる人向けの処理があるのでエンティティにロジックを持たせる 7 // 住所変更時に属性を更新する 8 if ($this->isIsland($newAddress)) { 9 $this->_properties['lives_island'] = true; 10 $this->_properties['default_transport_fee'] = $this->getDefaultTransportFee($newAddress); 11 } 12 } 13}
php
1class UserProfileController { 2 public function update() 3 { 4 // (いろいろ省略) 5 $user->updateProfile($this->request->data); 6 $this->Users->save($user); 7 // $user->profileも更新されてるけど dirty フラグが立っていないので保存されない 8 // $this->Prifile->save($user->profile); を実行してもいいけどそれよりも $this->Users->save($user) で一括保存したい 9 10 // $this->Users->patchEntity($user, $this->request->data) を実行すればdirtyフラグが立つのは知ってるけど、Profile::changeAddress() 内のロジックが実行されない。 11 } 12}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/20 00:50 編集
2018/11/20 00:58