Symfony2, Doctrine – get the primary key field name in controller

If you need the primary key name in the Symfony2 controller you can get in this way:

   public function setGridDataAction(Request $request)  
{
$em = $this->getDoctrine()->getEntityManager();
$meta = $em->getClassMetadata('AcmeTestBundle:TestTable');
$sPrimaryKeyName =$meta->getSingleIdentifierFieldName(); //get primary key name
$aAllField = $meta->getFieldNames(); //get all field names
foreach($aAllField as $sFieldName)
{
echo $sFieldName;
}
}

You will rarely need this bat sometimes you will need it 🙂

This entry was posted in Doctrine, get all field name, get primary key name, Symfony2. Bookmark the permalink.

3 Responses to Symfony2, Doctrine – get the primary key field name in controller

  1. Chris says:

    Thank you for this blog post. Just what I was looking for.Chris

  2. Anonymous says:

    I thank you too for this post!

  3. xjan says:

    You are welcome 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s