Guest User

Untitled

a guest
Aug 18th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. Using applyFilter for updates
  2. Users::applyFilter('save', function($self, $params, $chain) {
  3. if ($params['data']) {
  4. $params['entity']->set($params['data']);
  5. $params['data'] = array();
  6. }
  7. if (!$params['entity']->exists()) {
  8. $params['entity']->password = Password::hash($params['entity']->password);
  9. }
  10. return $chain->next($self, $params, $chain);
  11. });
  12.  
  13. $user = Users::first($id);
  14. if (!empty($this->request->data) && !empty($user)) {
  15. if ($user->save($this->request->data)) {
  16. // woohoo
  17. } else {
  18. // bummer
  19. }
  20. }
  21.  
  22. Users::applyFilter('save', function($self, $params, $chain) {
  23. if ($params['data']) {
  24. $params['entity']->set($params['data']);
  25. $params['data'] = array();
  26. }
  27. $entity = $params['entity'];
  28. if ($entity->password) {
  29. $export = $entity->export();
  30. if (empty($export['data']['password']) || $export['data']['password'] != $entity->password) {
  31. $entity->password = Password::hash($entity->password);
  32. }
  33. }
  34. return $chain->next($self, $params, $chain);
  35. });
Add Comment
Please, Sign In to add comment