Another smart way would be to use the actual /component/com_users/models/registration.php class method called register since it will take care of everything really.
First you add these methods to your helper class
/**
* Get any component s model
**/
public static function getModel($name, $path = JPATH_COMPONENT_ADMINISTRATOR, $component = yourcomponentname )
{
// load some joomla helpers
JLoader::import( joomla.application.component.model );
// load the model file
JLoader::import( $name, $path . /models );
// return instance
return JModelLegacy::getInstance( $name, $component. Model );
}
/**
* Random Key
*
* @returns a string
**/
public static function randomkey($size)
{
$bag = "abcefghijknopqrstuwxyzABCDDEFGHIJKLLMMNOPQRSTUVVWXYZabcddefghijkllmmnopqrstuvvwxyzABCEFGHIJKNOPQRSTUWXYZ";
$key = array();
$bagsize = strlen($bag) - 1;
for ($i = 0; $i < $size; $i++)
{
$get = rand(0, $bagsize);
$key[] = $bag[$get];
}
return implode($key);
}
Then you add the following user create method also to you component helper class
/**
* Greate user and update given table
*/
public static function createUser($new)
{
// load the user component language files if there is an error
$lang = JFactory::getLanguage();
$extension = com_users ;
$base_dir = JPATH_SITE;
$language_tag = en-GB ;
$reload = true;
$lang->load($extension, $base_dir, $language_tag, $reload);
// load the user regestration model
$model = self::getModel( registration , JPATH_ROOT. /components/com_users , Users );
// set password
$password = self::randomkey(8);
// linup new user data
$data = array(
username => $new[ username ],
name => $new[ name ],
email1 => $new[ email ],
password1 => $password, // First password field
password2 => $password, // Confirm password field
block => 0 );
// register the new user
$userId = $model->register($data);
// if user is created
if ($userId > 0)
{
return $userId;
}
return $model->getError();
}
Then anywhere in your component you can create a user like this
// setup new user array
$newUser = array(
username => $validData[ username ],
name => $validData[ name ],
email => $validData[ email ]
);
$userId = yourcomponentnameHelper::createUser($newUser);
if (!is_int($userId))
{
$this->setMessage($userId, error );
}
Doing it this way saves you all the trouble of handling the emails that needs to be send, since it automatically will use the system defaults. Hope this helps someone :)