【问题标题】:Doctrine and Function YEAR()教义和功能 YEAR()
【发布时间】:2015-02-28 21:55:59
【问题描述】:

您好,我正在使用 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();

请帮帮我

【问题讨论】:

  • 你能发布完整的类定义吗?我认为这个类中没有getEntityManager 方法
  • 我打赌你在控制器里:$em = $this->get('doctrine.orm.entity_manager')

标签: php symfony doctrine-orm


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多