How can I create a custom SQL query in Symfony2 using Doctrine? Or without Doctrine, I don’t care.
Doesn’t work like this:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can get the Connection object directly from the Entity Manager, and run SQL queries directly through that:
However, I’d advise against this unless it’s really necessary… Doctrine’s DQL can handle almost any query you might need.
Official documentation: https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html