Zoha Ali Khan
Zoha Ali Khan

Reputation: 1699

Using Query Builder in Symfony 2

I am trying to use Query Builder in Symfony2 to get some records from a database. I run the normal query in SQL and it returns the correct results. The query is

SELECT pg.name, pg.description
FROM pm_patentgroups pg
LEFT JOIN pm_portfolios pp ON pp.id = pg.portfolio_id

I want to use the exact query using Doctorine query builder in Symfony2. What I have tried so far is

 $repository = $this->getDoctrine()
        ->getRepository('MunichInnovationGroupBundle:PmPatentgroups');
        $query = $repository->createQueryBuilder('pg')
        ->from('pm_patentgroups', 'pg')
        ->leftJoin('pg','pm_portfolios','pp','pp.id = pg.portfolio_id')
        ->getQuery();
        $portfolio_groups = $query->getResult();

but its giving me the following error:

Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder()

I am new to Symfony2 and Doctorine. Can you please tell me what is going wrong here?

Thanks

Upvotes: 0

Views: 25871

Answers (2)

Fred Jiles
Fred Jiles

Reputation: 353

You are missing the alias when using createQueryBuilder. Since you have the repository you can drop the from portion and just use

$query = $repository->createQueryBuilder('pg')

Upvotes: 4

Cerad
Cerad

Reputation: 48893

Something like:

$qb = $this->getDoctrine()->createQueryBuilder();

$qb->addSelect('pm_patentgroups');
$qb->addSelect('pm_portfolios');

$qb->from('MunichInnovationGroupBundle:PmPatentgroups','pm_patentgroups');
$qb->leftJoin('pm_patentgroups.pm_portfolios','pm_portfolios');

This assumes you have your two entities properly related.

Lots of examples in the D2 manual. Just keep in mind that query builder works with objects, not sql.

And by the way, your error message comes from the fact that the entity repository (as opposed to the entity manager) requires an alias.

Upvotes: 2

Related Questions