Search code examples
symfonydoctrine-ormcustomizationdqlquery-builder

Query-builder in Symfony2, known function Month error


I have a problem with Symfony2 QueryBuilder.

I have the following code which throws an error

$repositorio= $em->getRepository('SingUserBundle:Data');
$consulta= $repositorio->createQueryBuilder('d')
    ->where('d.usuario = :usuario')
    ->andWhere ('MONTH(d.fecha) = :mesActual')
    ->setParameter('usuario', $usuario->getId())
    ->setParameter('mesActual', $mesActual)
    ->getQuery();
$totaldatos= $consulta->getSingleResult();

The error is:

[Syntax Error] line 0, col 80: Error: Expected known function, got 'MONTH'

Solution

  • Finally I fix up my problem! and finally I didn´t use MONTH

    $fecha=new \DateTime("now");
                $fecha->setDate(DATE_FORMAT($fecha, 'Y'),DATE_FORMAT($fecha, 'm'),01);
    
                $qb= $this->createQueryBuilder('ud')
                    ->where('ud.usuario = :identifier')
                    ->andWhere('ud.fecha >= :identifier2')
                   ->setParameters(array('identifier'=>$user,'identifier2'=> $fecha));
    

    I took the date one month laterand did the query.

    I hope to help someone!