gpt4 book ai didi

datetime - 如何将Doctrine2中的日期时间字段与日期进行比较?

转载 作者:行者123 更新时间:2023-12-03 22:05:50 26 4
gpt4 key购买 nike

我想获取今天由 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

最佳答案

一种想法是从日期中提取:年、月和日。然后

$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 和 YEAR 从中取出 DoctrineExtensions

例如

DoctrineExtensions

这对我有用。您只需要文件:day.php、month.php 和 year.php.....

你得到月份,例如:
    $datetime = new \DateTime("now");
$month = $datetime->format('m');
echo $month;

将 day.php、month.php 和 year.php 复制到您的包 Xy\TestBundle\Dql
在 app\config.yml 中注册新函数
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

关于datetime - 如何将Doctrine2中的日期时间字段与日期进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10681158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com