[英]Cast into a querybuilder
I have a querybuilder like this: 我有一个这样的querybuilder:
->andWhere('i.createdAt LIKE :date')
->setParameter('date', $date.'%')
The field created_at is a DateTime and my $date is a string. 字段created_at是DateTime,我的$ date是字符串。 Is there any way to cast my created_at as a string ?
有什么方法可以将我的created_at强制转换为字符串?
I've already tried : 我已经尝试过了:
->andWhere('i.createdAt::text LIKE :date')
Console : 安慰 :
.... has no field or association named createdAt::text
Or : 要么 :
->andWhere('to_char(i.createdAt) LIKE :date')
Console : 安慰 :
[Syntax Error] line 0, col 208: Error: Expected known function, got 'to_char'
Or 要么
[Syntax Error] line 0, col 208: Error: Expected known function, got 'cast'
I'm using Symfony 2.6 and postgreSQL. 我正在使用Symfony 2.6和postgreSQL。
Thanks you :) 谢谢 :)
format the date to a string like so: 将日期格式化为字符串,如下所示:
$date->format('Y-m-d H:i:s')
Or leave out the day and time since it's a LIKE query 还是省去日期和时间,因为这是LIKE查询
$date->format('Y-m-') . '%'
Consider however querying with dates between a start and an end point: 但是,请考虑使用起点和终点之间的日期进行查询:
$nextMonth = clone $date;
$nextMonth->modify('+1 month');
Then you can do a greater or equal to first date and less than or equal to the end date 然后,您可以大于或等于第一个日期,小于或等于结束日期
Thanks you, that's a great idea. 谢谢,这是个好主意。
$startInterval = \DateTime::createFromFormat('Y-m', $date);
$endInterval = clone $startInterval;
$endInterval->modify('+1 month');
This is a way better ! 这是更好的方法!
->andWhere('i.createdAt > :startInterval')
->andWhere('i.createdAt < :endInterval')
->setParameter('startInterval', $startInterval)
->setParameter('endInterval', $endInterval)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.