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

三世轮回 提交于 2020-01-07 03:46:11

问题


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.


回答1:


Alternatively you can load user as per username used to create user.

$return = $model->register($data);
if($return == 'useractivate'){
    $userid = JFactory::getUser($username)->id;
}


来源:https://stackoverflow.com/questions/39558859/how-to-return-the-user-id-from-the-joomla-framework-on-insert-update-a-new-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!