Get primary key id in inserted string in symfony

I have a symfony function that returns a rest api. I can successfully send data to a new record from the endpoint. This is a symfony controller as shown

$users->setUsername($request->request->get('username'));
$users->setPhonenumber($request->request->get('phonenumber'));
$users->setEmail($request->request->get('email'));
$users->setPassword($request->request->get('password'));
$users->setDateregistered(new \DateTime('now'));

$em->persist($users);
$em->flush();

Assuming the above inserts a new record into a new table, my task is to get the primary key of this table.

+4
source share
1 answer

try this after the flush method:

$userId = $users->getId();

You can do this because the doctrine hydrates the variable you pass

+8
source

Source: https://habr.com/ru/post/1689887/


All Articles