Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Symfony2: get the id of the persisted object

I have two entity: User and Person.

In the entity User I need the id of the associated person: user_id.

When I am creating a new user, I have to create first the person and then the user. In the user, I have to put the id of the corresponding person and for that I need to get the id of the persisted object person which is an auto increment.

Is it possible to get the id of the object after:

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

And how can I do this?

The alternative is to search the biggest id it the table Person and take this one but I think there should be a better and easier method to get the id of the persisted object.

In php for example, when I execute

$articleID = $_DB->queryRaw((....);

I am getting the id like that.

Upvotes: 39

Views: 40373

Answers (1)

Habibillah
Habibillah

Reputation: 28705

Symfony2 with Doctrine as default ORM will automatically generate an ID after data stored in database. So you can call the ID by ->getId()

$id = $person->getId();

Upvotes: 58

Related Questions