Ahmed.W
Ahmed.W

Reputation: 47

Doctrine and Function YEAR()

Hello i'm working in project with Symfony 2.4 i want to make a select using QueryBuilder but i have a exception :

FatalErrorException: Error: Call to undefined method getEntityManager() This is my code :

    $session=$this->get('session');
    $id_client=$session->get('client');
     $emConfig = $this->getEntityManager()->getConfiguration();
     $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
     $qb = $this->createQueryBuilder('f');
     $qb->select('SUM(f.montantTtc)');
     $qb->from('factureBundle:Facture', 'f');
     $qb->where('f.client = :id_client');
     $qb->groupBy('YEAR(f.dateEdition)');
     $qb->setParameter('id_client', $id_client);
     $factures_by_years=$qb->getQuery()->getResult();

plz help me

Upvotes: 1

Views: 5373

Answers (1)

Ahmed.W
Ahmed.W

Reputation: 47

here is the topic : https://simukti.net/blog/2012/04/05/how-to-select-year-month-day-in-doctrine2.html

and this is the code :

<?php

// .........
// Doctrine2 repository class for entity 'Post'
// .........
public function findOneByYearMonthDay($year, $month, $day)
{
    $emConfig = $this->getEntityManager()->getConfiguration();
    $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
    $emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
    $emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');

    $qb = $this->createQueryBuilder('p');
    $qb->select('p')
       ->where('YEAR(p.postDate) = :year')
       ->andWhere('MONTH(p.postDate) = :month')
       ->andWhere('DAY(p.postDate) = :day');

    $qb->setParameter('year', $year)
       ->setParameter('month', $month)
       ->setParameter('day', $day);

    $post = $qb->getQuery()->getSingleResult();
    return $post;
}

Upvotes: 1

Related Questions