I have developed a custom component that store some user information in separate tables, I want to store/update user information when user deleted or updated from admin section or front end.
1 Answer
You should save your data using a User Plugin - they're very easy to write. An example can be found at Creating a profile plugin, though the example might have more than you need.
The events your code would go into are onUserAfterSave
and onUserAfterDelete
and are documented at Plugin/Events/User. These events will fire when a user is saved or deleted in the admin or Front-end.
The guts of the plugin would be this:
defined('_JEXEC') or die;
class PlgUserMyPlugin extends JPlugin
{
protected $app;
protected $db;
public function onUserAfterDelete($user, $success, $msg) {
if (!$success)
return false;
$query = $this->db->getQuery(true)
->delete($this->db->quoteName('#__mytable'))
->where($this->db->quoteName('userid') . ' = ' . (int) $user['id']);
$this->db->setQuery($query)->execute();
return true;
}
public function onUserAfterSave($user, $isnew, $success, $msg) {
if (!$success)
return false;
// Update your table here
if ($isnew) {
// New User
}else{
// Existing User
}
}
}
-
thanks @GDP, I think this is the best way to achieve the required functionality without modifying core files. Nov 10, 2014 at 4:08