3

I've been learning web development with Symfony for some time now. I was following the tutorial on Symfony's website and Doctrine tutorial, and tried to use entity manager in the Doctrine ORM to create queries but PhpStorm's autocompletion tells me that it couldn't find createquery(). (see below for screenshot)

enter image description here

enter image description here

LazyOne
  • 158,824
  • 45
  • 388
  • 391
yatsky
  • 95
  • 1
  • 10
  • If you learning good practices for Symfony, you have to get repository before create the query – Charlie Lucas Apr 25 '18 at 09:57
  • You don't need to get the repo first. It's common but not required. The repo just passes the call up to the entity manager anyways. – Cerad Apr 25 '18 at 12:16

2 Answers2

1

Are you sure you have included the following use statement

use Doctrine\ORM\EntityManager
  • Please replace 'use Doctrine\Common\Persistence\ObjectManager' with 'use Doctrine\ORM\EntityManager', then you will be able to find method createQuery() – Shaithya Kannan Apr 25 '18 at 09:08
1

As per your screenshot (getRepository, flush, persist...) are methods of $this->getDoctrine()->getManager(), that you can found on any symfony controller class :

enter image description here

What you want to use is $this->getEntityManager()->createQuery() that you can found on symfony repository class :

enter image description here

So the proper way doing this is to create a Repository and add your method into it, then use this method inside your controller :

class UserController extends Controller
{

  /**
   * @Route("/", name="user_list")
   */
  public function indexAction()
  {
      $em = $this->getDoctrine()->getManager();
      $userrs = $em->getRepository("AppBundle:User")->myRepositoryMethod();
      ...
RedaMakhchan
  • 482
  • 3
  • 9