Getting an ID from newly created user

How to retrieve the ID of a newly created Joomla user in ChronoForms.

Overview

The issue occurs when using a CF form to create a user and a user group, as the user ID is not directly accessible in the form's processing code.
Access the user ID from the Joomla user object created by the form action. Use the correct property path to retrieve the ID for further operations like adding the user to a group.

Answered
ChronoForms v8
st stwgroup 01 Apr, 2024
I am using the Joomla save user form.
I'm creating a new user and creating a new group using the account_name variable.
Under the submit tab, query the db to get the newgroupID. (Could not figure out how to use joomla to do it)
What I'm having trouble with is getting the id of the newly created user. Below is the code in the php9 block and the debug output.
All assistance with getting the userid and properly creating the group would be appreciated..

Vars
Array
(
    [app_active_page] => 0
    [joomla_user7_activation_token] => 
    [joomla_user7_activation_url] => https://stwgroup.online/index.php?option=com_users&view=registration&task=registration.activate&token=
    [joomla_user7] => Array
        (
            [name] => site1admin33333
            [username] => site1admin33333
            [email] => wscottchris+scoredit3333@gmail.com
            [password] => $2y$10$bc1tn5hbjT1Pg4fDIl3J5e8C6nK3yBKpUn0zlyREyS4g0WxwID4Tq
            [block] => 0
            [activation] => 
            [registerDate] => 2024-04-01 13:12:22
            [params] => {}
            [id] => 375
        )

    [php9] => NULL
)


use Joomla\CMS\Factory;
use Joomla\Component\Users\Administrator\Model\GroupModel;
use Joomla\CMS\User\UserHelper;

$cms = Factory::getApplication('site');

$newTitle = str_replace(' ', '_', $this->data("account_name", ""));
$newUserID = $this->joomla_user7->id;

$groupData = [
    'id' => 0, 
    'title' => $newTitle, 
    'parent_id' => 2
];

    try {
        // Create a new instance of the GroupModel
        $groupModel = new GroupModel();


        // Save the group to the database
        $groupModel->save($groupData);

        // Retrieve the ID of the newly created group
        $db = Factory::getContainer()->get('DatabaseDriver');
		$groupID = $db->getQuery(true);
		$groupID->select($db->quoteName('id'))
		->from($db->quoteName('#__usergroups'))
		->where($db->quoteName('title') . ' = ' . $db->quote($newTitle));
		$db->setQuery($groupID);
		$data = $db->loadObject();
        $newgroupID = $data->id;
        // Now $newGroupId holds the ID of the newly created group;
		
		
		// add new user to newly created group
		if (!UserHelper::addUserToGroup($newUserID, $newgroupID)) {
		return ;
		}	
        return $newUserID;
    } catch (\Exception $e) {
        // Handle any exceptions that occur during the save operation
        // You can log the error or return a default value, depending on your application's requirements
        return null;
    }
Max_admin Max_admin 02 Apr, 2024
Answer
$this->get("joomla_user7.id")
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.