Search code examples
typo3typo3-8.xtypo3-extensions

How to create a new TYPO3 FE User in an Extbase-CommandController-Task


We need to synchronise users and therefore plan to pull data from a third party API with the TYPO3 scheduler that updates existing users and adds new users to the fe_users table.

Updating existing users works fine, but I don't know how to create a new user. Here is the code I currently use in TYPO3 v8. I don't get any error messages, but the new user is not added to the database.

/**
 * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
 */
protected $userRepository;

/**
 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
 */
protected $persistenceManager;

...

$user = $this->userRepository->findOneByForeignId($id);
if ($user) {
    $this->updateUserData($user);
    $this->userRepository->update($user);
} else {
    $user = new \TYPO3\CMS\Extbase\Domain\Model\FrontendUser();
    $this->updateUserData($user);
    $this->userRepository->add($user);
}
$this->persistenceManager->persistAll();

...

private function updateUserData(&$user)
{
    $user->setFirstName($this->firstName);
    $user->setLastName($this->lastName);
    ...
}

I couldn't find any example that creates a new object in a loop. They (here user models) are always injected into the controller action as a parameter.


Solution

  • According to your code, I could not tell why the new user could not be created. But you can refer to the log, hopefully it will provide you some tips.

    Generally, there could be 2 reasons. One is the pid. As you can update the existing users, I think it's not a problem. The other one is the validation. You can not insert a record if the SQL fails.

    BTW, I have some similar extensions to do the same things, but not for fe_users table, and they all works.