0

I am trying to get objects from DQL query. Here is my code :

$em = $this->getDoctrine()->getRepository(Item::class);

$items = $em->createQuery($getQuery);
$items = $query->getResult();

$getQuery = DQL query string : SELECT from Entity WHERE ...

I am receiving error : Undefined method 'createQuery'. The method name must start with either findBy or findOneBy! I don't understand it, bcz this example is copied from official documentation. How I can execute DQL query in queryBuilder/createQuery?

7
  • Does Item::class entity class have a repository? Commented Sep 23, 2017 at 22:19
  • Yes. namespace AppBundle\Repository; use Doctrine\ORM\EntityRepository; /** * ItemRepository * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class ItemRepository extends EntityRepository { } Commented Sep 23, 2017 at 22:20
  • What is in getDoctrine()? Commented Sep 23, 2017 at 22:26
  • What do you mean @OluwafemiSule ? Nothing inside ( ). Commented Sep 23, 2017 at 22:27
  • I mean the method body for getDoctrine(). Did you write it? Commented Sep 23, 2017 at 22:28

1 Answer 1

1

Entity manager has a createQuery method.

$em = $this->getDoctrine()->getManager();

$query = $em->createQuery($getQuery);

$items = $query->getResult();

Repository has a createQueryBuilder method.

$em = $this->getDoctrine()->getManager();

$qb = $em->getRepository(Item::class)->createQueryBuilder();
$query = qb->select('[columns]')->from('Entity')->where('[condition]')->getQuery();
$items = $query->getResult();
Sign up to request clarification or add additional context in comments.

6 Comments

Some progress. What method is used to get results? Attempted to call an undefined method named "getResult" of class "Doctrine\ORM\EntityManager". 500 Internal Server Error - UndefinedMethodException
My IDE doesn't see method createQuery. Something is wrong with USE maybe?
Referenced method is not found in subject class.
createQuery is called on an entity manager while execute on a query object
If you're using a repository, you can build a query with a query builder using createQueryBuilder and subsequent query builder methods
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.