Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method 'QueryBuilder' not found in class ObjectRepository

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

like image 261
John Doe Avatar asked Jun 06 '14 12:06

John Doe


1 Answers

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();
     // ...
}
like image 89
Rybus Avatar answered Nov 05 '22 17:11

Rybus