Method 'QueryBuilder' not found in class ObjectRepository
Asked Answered
T

1

5

New to Symfony & Doctrine

I trying to fetch a selection of objects from a MySQL database via Doctrine in a Symfony project. I am doing this with createQueryBuilder;

$repository = $this->getDoctrine()
    ->getRepository('mcsyncBundle:DB');
$query = $repository->createQueryBuilder('p')
    ->select('p')
    ->where('(p.created=:)' . $till)
    ->setParameter('created', $till)
    ->orderBy('p.created', 'ASC')
    ->getQuery();
$PackDB = $query->getResult();

But I keep getting the error:

*Method 'QueryBuilder' not found in class \Doctrine\Common\Persistence\ObjectRepository*.

Anyone that can explain (fix) this problem?

EDIT: This error is coming from inside PHPStorm by the way and NOT from Symfony itself

Trainor answered 6/6, 2014 at 12:17 Comment(0)
V
9

I suppose you wrote that code in a Controler file. QueryBuilders are meant to be in Repository files. For /Entity/Plop.php, you should have /Entity/PlopRepository.php as well.

PlopRepository.php

namespace Foo\BarBundle\Entity;
use Doctrine\ORM\EntityRepository;

class PlopRepository extends EntityRepository
{
    public function getCreatedP()
    {
        $qb = $this->createQueryBuilder('p')
                  ->select('p')
                  ->where('p.created = :created')
                  ->setParameter('created', $till)
                  ->orderBy('p.created', 'ASC');

        return $qb->getQuery()
                  ->getResults();
     }
     // ...
}

EDIT 1 : and there was a mistake in your ->where('...') statement I fixed ;)

EDIT 2 : to be complete, controller part :

TotoController.php

public function getPlopAction() 
{
     $entityManager = $this->getDoctrine()->getManager();
     $plopRepository = $entityManager->getRepository('FooBarBundle:Plop');  
     $plops = $plopRepository->getCreatedP();
     // ...
}
Vituperate answered 6/6, 2014 at 12:34 Comment(3)
And the TS said; "Let there be light!" (Thanks for explanation and the statement fix!)Trainor
Great ! Be careful, there was an extra getQuery() call in my answer. I edited my post ;)Vituperate
I added controller part. Can't help myself improving my answers :)Vituperate

© 2022 - 2024 — McMap. All rights reserved.