3

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.

2
  • In regards to code, show us how you currently are doing it.
    – Lodder
    Nov 9, 2014 at 10:41
  • Its basic functionality to update the custom component table when user database updated . Nov 9, 2014 at 14:02

1 Answer 1

6

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
        }
    }
}
1
  • thanks @GDP, I think this is the best way to achieve the required functionality without modifying core files. Nov 10, 2014 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.