如何在插入/更新(新)用户时从Joomla框架返回user_id [英] How to return the user_id from the Joomla-Framework on insert/update a (new) user

查看:123
本文介绍了如何在插入/更新(新)用户时从Joomla框架返回user_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从外部脚本向joomla页面插入/更新新用户.以下代码效果很好.但是,我需要添加或更新的用户的user_id,但脚本仅返回"useractivate".

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".

Joomla论坛/文档( https://github.com/joomla/joomla- cms/issues/9644 )建议使用

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());

并得到结果:0

创建用户

<?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:我必须使用joomla-framework,并且不能使用简单的mysql查询,因为新用户必须激活其帐户并轻松设置新密码.

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

这篇关于如何在插入/更新(新)用户时从Joomla框架返回user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆