I try to insert/update a new user to a joomla page from an external script. The following code works very well. However I'll need the user_id of the added or updated user but the script returns just "useractivate".
The Joomla Forum/Documentation (https://github.com/joomla/joomla-cms/issues/9644) suggests to use
$db->insertid();
But I was not able to get this work. Up to now I tried this:
$db = JFactory::getDbo();
var_dup($db->insertid());
and get the result: 0
User Creation
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', __DIR__ );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$language = JFactory::getLanguage();
$language->load('com_users', JPATH_BASE);
JFactory::getLanguage()->load('mod_login', JPATH_BASE, 'de-DE', true);
$app = JFactory::getApplication('site');
$app->initialise();
require_once(JPATH_BASE.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php');
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
jimport('joomla.user.helper');
$username = 'jimporttest';
$name = 'J Port2';
$email = 'test@test.de';
$password = 'test';
$data = array(
'username' => $username,
'name' => $name,
'email1' => $email,
'password1' => $password,
'password2' => $password,
'block' => 0
);
$return = $model->register($data);
var_dump($return);
PS: I have to use the joomla-framework and can not use a simple mysql query because the new user has to activate its account and set easily a new password.
Alternatively you can load user as per username used to create user.
$return = $model->register($data);
if($return == 'useractivate'){
$userid = JFactory::getUser($username)->id;
}