Extending the JUser class
Thursday, 28 May 2009 17:22

I was in the process of upgrading one of my components (the com_tracker) to the 1.5 version and I've got to the point where I needed to update the #__users table in my component.This components adds some new fields to the user table so the standard JUser class only updates the standard fields.

In the old version I developed my own function to save, create, delete, whatever in the user table but now with the MVC structure I said to myself that I was going to reuse the core code and not re-develop all the functions that were already done.

So I went to the forum (second best friend after Google) and with the help of Ian I was able to sucessfully extend the JUser class to accomodate my own needs.

Here's a quick How-To (far from complete) on how to extend the JUser class:

 

A quick hint how this should be used :

1. create a extended user database table, either by adding fields to the existing table or by create a new table and using the user_id to create a relationship

2. create an extended JTableUser class, that either uses joins or subqueries to work with the extra table or that is capable of handling the fields added to the existing table.

3. user JUser::getTable either statically or dynamically and feed it the name of you new user table class. Like

Code:
JUser::getTable('tablename');


4. in case the new user table class is not in joomla.database.table, you will need to tell JTable where to find it using JTable::addIncludePath.

Use cases :

The changes allow you to create a thicker JUser object that can be used in your code. You can choose to either only expose it to your own component by doing the above last two steps only for you component. Or you could create a system plugin which set the database table name and exposed your extended JUser object to the whole system.

In /administrator/components/com_younameit/tables/myuser.php

<?php
defined('_JEXEC') or die('Restricted access');
JLoader::register('JTableUser', JPATH_LIBRARIES.DS.'joomla'.DS.'database'.DS.'table'.DS.'user.php');

class JTableMyUser extends JTableUser {

  var $country=null;
  var $city=null;
  var $photo=null;

   function __construct(&$db) {
      parent::__construct( $db );
   }

   function load($id) {
      return parent::load($id);
   }
}

And in /components/com_younameit/views/users/view.html.php

<?php
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view' );

class MyComponentViewMyUsers extends JView
{

  function display($tpl = null) {
      global $mainframe, $option;
      $db          =& JFactory::getDBO();
      JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_younameit'.DS.'tables');

         $user = JUser::getTable('MyUser');
         $session  =& JFactory::getSession();
         $user->load($session->get('user')->id);
...

So now you can use $user->country or $user->city or $user->photo and It will fetch the values from your own extended user table.

 

Final note:

I'm still working on the component so I still don't have any example for the save, edit or new functions (the delete it's the same since it's by user id).