Skip to content

Instantly share code, notes, and snippets.

@marzocchi
Created August 2, 2012 15:12
Show Gist options
  • Save marzocchi/3237804 to your computer and use it in GitHub Desktop.
Save marzocchi/3237804 to your computer and use it in GitHub Desktop.
Change a Doctrine entity's table name at runtime
<?php
class FooController extends Controller {
function fooAction() {
$em = $this->getDoctrine()->getEntityManager();
$cm = $em->getClassMetadata('FooBundle:FooEntity');
$cm->setTableName('special_table_name');
$repo = $em->getRepository('FooBundle:FooEntity');
$entities = $repo->createQueryBuilder('f')
->setMaxResults(1)
->orderBy('f.id', 'desc')
->getQuery()
->getResult();
return new Response('');
}
}
@omar331
Copy link

omar331 commented Mar 6, 2018

Great!

@duepi
Copy link

duepi commented Oct 25, 2019

Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment