Error in my createQueryBuilder

I have a query in my repository, this is my function:

public function queryActivos()
    {
        $em = $this->getEntityManager();
        $query = $em->createQueryBuilder('af')
            ->where('af.activo = :activo')
            ->setParameter('activo', true);
        return $query;
    }

I call this function from my formType:

->add('articulosDefectuosos','entity', array(
                'empty_value' => 'SELECCIONE ARTICULO DEFECTUOSO',
                'class'     => 'VentasAlmacenBundle:ArticuloDefectuoso',
                'multiple' => true,
                'expanded' => false,
                'query_builder' => function(EntityRepository $er){
                    return $er->queryActivos();
                },
                'attr'  =>  array(
                    'class' => "browser-default"
                )
            ))

This get the next error in the queryBuilder:

Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'WHERE'
500 Internal Server Error - QueryException 

Where is the problem??

Upvotes: 0

Views: 902

Answers (1)

xabbuh
xabbuh

Reputation: 5881

Your query is missing which data to select and from which entity to select the data. Usually, you would add these missing piece of information by calling the select() and from() methods of the query builder. However, since you are creating the query builder inside your repository you can use its createQueryBuilder() method directly which adds the missing information automatically:

public function queryActivos()
{
    $queryBuilder = $this->createQueryBuilder('af')
        ->where('af.activo = :activo')
        ->setParameter('activo', true);
    return $queryBuilder;
}

see also http://symfony.com/doc/current/book/doctrine.html#querying-for-objects-using-doctrine-s-query-builder

Upvotes: 1

Related Questions