主义与功能年份()

时间:2015-02-28 21:56:00

标签: php symfony doctrine-orm

您好我在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();

请帮帮我

1 个答案:

答案 0 :(得分:1)

这是主题: 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;
}