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)
Asked
Active
Viewed 2,114 times
3
-
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 Answers
1
Are you sure you have included the following use statement
use Doctrine\ORM\EntityManager

Shaithya Kannan
- 142
- 9
-
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 :
What you want to use is $this->getEntityManager()->createQuery() that you can found on symfony repository class :
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
-
Oh cheers, this is working now. So this means that I only define queries in **repository** right? – yatsky Apr 27 '18 at 00:05
-