@@ -29,13 +29,20 @@ class UserModel extends Model
2929 ];
3030 protected $ useTimestamps = true ;
3131 protected $ afterFind = ['fetchIdentities ' ];
32+ protected $ afterInsert = ['saveEmailIdentity ' ];
33+ protected $ afterUpdate = ['saveEmailIdentity ' ];
3234
3335 /**
3436 * Whether identity records should be included
3537 * when user records are fetched from the database.
3638 */
3739 protected bool $ fetchIdentities = false ;
3840
41+ /**
42+ * Save the User for afterInsert and afterUpdate
43+ */
44+ protected ?User $ tempUser = null ;
45+
3946 /**
4047 * Mark the next find* query to include identities
4148 *
@@ -53,7 +60,7 @@ public function withIdentities(): self
5360 * returned from a find* method. Called
5461 * automatically when $this->fetchIdentities == true
5562 *
56- * Model event callback called `afterFind`.
63+ * Model event callback called by `afterFind`.
5764 */
5865 protected function fetchIdentities (array $ data ): array
5966 {
@@ -185,71 +192,107 @@ public function activate(User $user): void
185192 {
186193 $ user ->active = true ;
187194
188- $ this ->saveWithEmailIdentity ($ user );
195+ $ this ->save ($ user );
189196 }
190197
191198 /**
192- * Override the BaseModel's `save()` method to allow
193- * updating of user email, password, or password_hash fields
194- * if they've been modified.
195- *
196199 * @param User $data
197200 *
198201 * @throws ValidationException
202+ *
203+ * @retrun true|int|string Insert ID if $returnID is true
199204 */
200- public function save ($ data): bool
205+ public function insert ($ data = null , bool $ returnID = true )
201206 {
202207 assert ($ data instanceof User);
203208
204- $ this ->saveWithEmailIdentity ( $ data) ;
209+ $ this ->tempUser = $ data ;
205210
206- return true ;
211+ $ result = parent ::insert ($ data , $ returnID );
212+
213+ $ this ->checkQueryReturn ($ result );
214+
215+ return $ returnID ? $ this ->insertID : $ result ;
207216 }
208217
209218 /**
210- * Save User and its Email Identity (email, password, or password_hash fields)
211- * if they've been modified.
219+ * @param array|int|string|null $id
220+ * @param User $data
212221 *
213222 * @throws ValidationException
214223 */
215- public function saveWithEmailIdentity ( User $ data ): void
224+ public function update ( $ id = null , $ data = null ): bool
216225 {
226+ assert ($ data instanceof User);
227+
228+ $ this ->tempUser = $ data ;
229+
217230 try {
218231 /** @throws DataException */
219- $ result = parent ::save ( $ data );
232+ $ result = parent ::update ( $ id , $ data );
220233 } catch (DataException $ e ) {
221234 $ messages = [
222- lang ('Database.emptyDataset ' , ['insert ' ]),
223235 lang ('Database.emptyDataset ' , ['update ' ]),
224236 ];
237+
225238 if (in_array ($ e ->getMessage (), $ messages , true )) {
226- // Save updated email identity
227- $ user = $ data ;
228- $ user ->saveEmailIdentity ();
239+ $ this ->tempUser ->saveEmailIdentity ();
229240
230- return ;
241+ return true ;
231242 }
232243
233244 throw $ e ;
234245 }
235246
236- if ($ result ) {
237- if ($ data ->id === null ) {
238- // Insert
239- /** @var User $user */
240- $ user = $ this ->find ($ this ->db ->insertID ());
241-
242- $ user ->email = $ data ->email ?? null ;
243- $ user ->password = $ data ->password ?? '' ;
244- $ user ->password_hash = $ data ->password_hash ?? '' ;
245- } else {
246- // Update
247- $ user = $ data ;
248- }
247+ $ this ->checkQueryReturn ($ result );
248+
249+ return true ;
250+ }
251+
252+ /**
253+ * Override the BaseModel's `save()` method to allow
254+ * updating of user email, password, or password_hash fields
255+ * if they've been modified.
256+ *
257+ * @param User $data
258+ *
259+ * @throws ValidationException
260+ */
261+ public function save ($ data ): bool
262+ {
263+ assert ($ data instanceof User);
264+
265+ $ result = parent ::save ($ data );
266+
267+ $ this ->checkQueryReturn ($ result );
268+
269+ return true ;
270+ }
271+
272+ /**
273+ * Save Email Identity
274+ *
275+ * Model event callback called by `afterInsert` and `afterUpdate`.
276+ */
277+ protected function saveEmailIdentity (array $ data ): array
278+ {
279+ // Insert
280+ if ($ this ->tempUser ->id === null ) {
281+ /** @var User $user */
282+ $ user = $ this ->find ($ this ->db ->insertID ());
283+
284+ $ user ->email = $ this ->tempUser ->email ?? '' ;
285+ $ user ->password = $ this ->tempUser ->password ?? '' ;
286+ $ user ->password_hash = $ this ->tempUser ->password_hash ?? '' ;
249287
250288 $ user ->saveEmailIdentity ();
289+
290+ return $ data ;
251291 }
252292
253- $ this ->checkQueryReturn ($ result );
293+ // Update
294+ $ this ->tempUser ->saveEmailIdentity ();
295+
296+ return $ data ;
254297 }
255298}
0 commit comments