如何在Doctrine2

时间:2016-08-17 12:24:35

标签: doctrine-orm symfony

我使用此代码选择对象案例MONTH 这是我在存储库中的功能

    public function findProductByCustomerIdGroupByCodeMc($id,$magasin,$periode)
    {

    $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('lead')
                    ->select('lead')
                    ->from('BOGeneralBundle:EtLeads', 'l')
                     if($periode != "0") 
                     {
                      $qb->andWhere('MONTH(l.dataCreated) = :periode')
                             ->setParameter('periode', $periode);
                     }
        return$qb->getQuery()->getResult();
    }

并进行传导,因为我有所有月份的选项,它的值= 0 并且页面显示此消息

  

致命错误:Class' DoctrineExtensions \ Query \ Mysql \ Month'找不到

1 个答案:

答案 0 :(得分:3)

对于Doctrine2,您必须注册自己的函数以在DQL中识别YEAR()MONTH()和DAY()

要执行此操作,您可以通过composer安装this个捆绑包。只需将其添加到composer.json然后php composer.phar update beberlei/DoctrineExtensions

即可
"beberlei/DoctrineExtensions": "*",

然后您可以将功能注册到您的ORM

doctrine:
    orm:
      auto_generate_proxy_classes: %kernel.debug%
      entity_managers:
        default:
          auto_mapping: true
          dql:
            datetime_functions:
              DAY: DoctrineExtensions\Query\Mysql\Day
              MONTH: DoctrineExtensions\Query\Mysql\Month
              YEAR: DoctrineExtensions\Query\Mysql\Year

This文章也谈到了这一点。