How to return the user_id from the Joomla-Framework on insert/update a (new) user

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值