如何从Symfony 2.2中的时间之间获取记录

时间:2015-12-29 07:13:09

标签: php symfony

我需要获得创建给定时间的记录: 我的SQL表如下

Id | Createdat 
1    2015-12-28 19:32:40
2    2015-12-29 19:35:05
3    2015-12-29 16:33:15
4    2015-12-28 19:32:36

我需要19:30:0019:35:00之间的结果,所以我只得到三个记录,如

    Id | Createdat 
    1    2015-12-28 19:32:40
    2    2015-12-29 19:35:05
    4    2015-12-28 19:32:36

我使用Symfony 2.2

我在存储库中的功能是:

 $results = $this->createQueryBuilder('t')
            ->select('t')
            ->where('CAST( t.createdAt  ) BETWEEN :starttime AND :endtime')
            ->andWhere('t.status = :status')
            ->setParameter('starttime', $starttime->format('H:i:s'))
            ->setParameter('endtime', $edntime->format('H:i:s'))
            ->setParameter('status', 1)
            ->getQuery()
            ->getResult();

但是我收到了这个错误

[Syntax Error] line 0, col 71: Error: Expected known function, got 'CAST' 

请帮忙

1 个答案:

答案 0 :(得分:0)

将此添加到您的config.yml

  orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    dql:
        datetime_functions:
            date:           Oro\ORM\Query\AST\Functions\SimpleFunction
            time:           Oro\ORM\Query\AST\Functions\SimpleFunction
            timestamp:      Oro\ORM\Query\AST\Functions\SimpleFunction
            convert_tz:     Oro\ORM\Query\AST\Functions\DateTime\ConvertTz
        numeric_functions:
            timestampdiff:  Oro\ORM\Query\AST\Functions\Numeric\TimestampDiff
            dayofyear:      Oro\ORM\Query\AST\Functions\SimpleFunction
            dayofmonth:     Oro\ORM\Query\AST\Functions\SimpleFunction
            dayofweek:      Oro\ORM\Query\AST\Functions\SimpleFunction
            week:           Oro\ORM\Query\AST\Functions\SimpleFunction
            day:            Oro\ORM\Query\AST\Functions\SimpleFunction
            hour:           Oro\ORM\Query\AST\Functions\SimpleFunction
            minute:         Oro\ORM\Query\AST\Functions\SimpleFunction
            month:          Oro\ORM\Query\AST\Functions\SimpleFunction
            quarter:        Oro\ORM\Query\AST\Functions\SimpleFunction
            second:         Oro\ORM\Query\AST\Functions\SimpleFunction
            year:           Oro\ORM\Query\AST\Functions\SimpleFunction
            sign:           Oro\ORM\Query\AST\Functions\Numeric\Sign
            pow:            Oro\ORM\Query\AST\Functions\Numeric\Pow
        string_functions:
            group_concat:   Oro\ORM\Query\AST\Functions\String\GroupConcat
            concat_ws:      Oro\ORM\Query\AST\Functions\String\ConcatWs
            cast:           Oro\ORM\Query\AST\Functions\Cast      

,您可以从日期范围中选择

    $beginDate = new \DateTime($beginDate.'-'.$month.'-'.$year);
    $endDate = new \DateTime($endDate.'-'.$month.'-'.$year);
    $qb = $this->getEntityManager()->CreateQueryBuilder();
    $q  = $qb->select('count(i)')
    ->from('CrmSandboxBundle:Invoice', 'i')
    ->leftJoin('i.user','u')
    ->where(
       'i.date BETWEEN :begin AND :end'
       )
    ->andWhere(
            'u.id = :user'
        )
    ->setParameter('begin', $beginDate, \Doctrine\DBAL\Types\Type::DATETIME)
    ->setParameter('end', $endDate, \Doctrine\DBAL\Types\Type::DATETIME)
    ->setParameter('user', $user->getId())
    ->getQuery();
    return  $q->getSingleScalarResult();