Float
Float

Reputation: 263

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.

Upvotes: 4

Views: 1490

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

try this after flush method:

$userId = $users->getId();

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

Upvotes: 8

Related Questions