Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the primary key id of the inserted row in symfony

I have a symfony function that returns a rest api. I can post data to a new record successfully from an endpoint. This is the 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 that the above inserts a new record successfully into a new table, my challenge is to get the primary key of that table.

like image 269
Float Avatar asked Jan 29 '23 03:01

Float


1 Answers

try this after flush method:

$userId = $users->getId();

You can do this because doctrine hydrate the variable that you pass

like image 69
Alessandro Minoccheri Avatar answered Jan 31 '23 18:01

Alessandro Minoccheri