12

我想获取今天由 Doctrine2 中的 QueryBuilder 创建的项目。我想将 createdAt(Datetime) 字段与今天的参数(Date) 进行比较。是否可以在一个查询中做到这一点?

$qb = $this->createQueryBuilder('i');
$qb->innerJoin('i.type', 'it');
$qb->andWhere('it.name = :type');
$qb->andWhere('i.createdAt < :today');
// i.createdAt == datetime and :today parameter is a date
4

6 回答 6

23

one idea is to extract from the date: the year, month and day. And then

$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);

MONTH DAY, and YEAR you take out the DoctrineExtensions from

e.g.

DoctrineExtensions

This works for me. You only need the files: day.php, month.php and year.php.....

You get the month e.g.:

    $datetime = new \DateTime("now");
    $month = $datetime->format('m');
    echo $month;

Copy day.php, month.php and year.php to your bundle Xy\TestBundle\Dql Register the new functions in app\config.yml with

doctrine:


orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            auto_mapping: true
            dql:
                datetime_functions:
                    month: Xy\TestBundle\Dql\Month
                    year: Xy\TestBundle\Dql\Year
                    day: Xy\TestBundle\Dql\Day
于 2012-05-21T14:34:38.507 回答
17

有比添加日期的学说扩展更好的选择。

首先,您需要获取 start-datetime 和 end-datetime :

$today_startdatetime = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
$today_enddatetime = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

现在在查询中使用它们:

$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('wsh')
    ->from('\Entities\wishes', 'wsh')
    ->where('wsh.created_at >= :today_startdatetime')
    ->andWhere('wsh.created_at <= :today_enddatetime');


$q_1->setParameter('today_startdatetime', $today_startdatetime);
$q_1->setParameter('today_enddatetime', $today_enddatetime);
$result= $q_1->getQuery ()->getResult ();
于 2014-10-21T09:17:18.260 回答
15

DATE_DIFF(date1, date2)也可以使用返回天数差异的内置函数。检查文档

$result = $this->createQueryBuilder('l')
    ->where('DATE_DIFF(l.startDate, CURRENT_DATE()) = 0')
于 2015-05-23T18:55:43.887 回答
13

如此成熟的 ORM 不提供该DATE 功能的情况很少见。但是,要从日期时间字段中获取日期,您可以应用如下SUBSTRING函数:

SELECT SUBSTRING(i.dateTimeField,1,10) FROM tableName i

希望能帮助到你!

于 2013-07-09T19:56:32.417 回答
5

您必须将today参数添加到查询 QueryBuilder 中。

$today = new \DateTime();
$qb->setParameter('today', $today->format('Y-m-d'));

使用 QueryBuilder,您可以将日期与 DateTime 格式进行比较'Y-m-d'

于 2012-05-21T09:57:55.160 回答
3

一个简单的解决方案是相应地格式化日期时间

public function getWithStartingPremium(\DateTime $datetime)
{
    $qb = $this->createQueryBuilder('c');

    return $qb
        ->innerJoin('c.collegePremium', 'cp')
        ->where($qb->expr()->eq('cp.start', ':date'))
        ->setParameters(array(
            'date' => $datetime->format('Y-m-d'),
        ))
        ->getQuery()
        ->getResult();
}
于 2015-11-12T11:08:44.923 回答