Last Updated: February 25, 2016
·
2.531K
· pleone

Symfony2 Entity Manager - Basic Operations

Some easy snippets ...

  1. howto get the EM :

    $em = $this->get('doctrine')->getEntityManager();

  2. howto quick find:

    $this->getDoctrine()->getRepository('DemoUserBundle:User')->find(1);

  3. persist data :

    $user = new User();
    $user->setName("asd");
    $em->persist($user);
    try {
    $em->flush();
    } catch (\Exception $exc) {
    //something...
    }

Note: \Exception and NOT Exception.

2 Responses
Add your response

Since Symfony 2.1 you should use getManager() instead of getEntityManager().

over 1 year ago ·

thnks for quick tip (y)

over 1 year ago ·