条令与职能YEAR()


Doctrine and Function YEAR()

你好,我在Symfony 2.4的项目中工作,我想使用QueryBuilder进行选择,但我有一个例外:

FatalErrorException:错误:调用未定义的方法getEntityManager()这是我的代码:

    $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帮助我

这里是主题:https://simukti.net/blog/2012/04/05/how-to-select-year-month-day-in-doctrine2.html

这是代码:

<?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;
}