日期无法从 Querybuilder 格式化为字符串
我使用 Symfony 5 中的 Querybuilder 并有两个日期列。在 JSON 字符串中,日期不仅仅是一个字符串。我尝试将 DATE_FORMAT 添加到选择中,但它不起作用。
我当前的代码是:
public function getTutoriallist(ManagerRegistry $doctrine, SerializerInterface $serializer): Response
{
$response = new JsonResponse();
$response->setCharset('UTF-8');
$response->setEncodingOptions(JSON_PRETTY_PRINT);
$query = $this->getDoctrine()
->getRepository('App:GrindingappTutorials')
->createQueryBuilder('t')
->select(['t.id', 't.headline', 't.descriptiontext'])
->addSelect(['t.adddate'])
->addSelect(['t.changedate'])
->getQuery();
$response->setData($query->getArrayResult());
return $response;
}
JSON-Response 看起来像
[
{
"id": 1,
"headline": "How to messure the T8",
"descriptiontext": "Learn how to messure the T8",
"adddate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"changedate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
I use the Querybuilder in Symfony 5 and have two date columns. In the JSON String the date would be not only one string. And I had tryied to add DATE_FORMAT to the select, but it would not work.
My current code is:
public function getTutoriallist(ManagerRegistry $doctrine, SerializerInterface $serializer): Response
{
$response = new JsonResponse();
$response->setCharset('UTF-8');
$response->setEncodingOptions(JSON_PRETTY_PRINT);
$query = $this->getDoctrine()
->getRepository('App:GrindingappTutorials')
->createQueryBuilder('t')
->select(['t.id', 't.headline', 't.descriptiontext'])
->addSelect(['t.adddate'])
->addSelect(['t.changedate'])
->getQuery();
$response->setData($query->getArrayResult());
return $response;
}
and the JSON-Response looks like
[
{
"id": 1,
"headline": "How to messure the T8",
"descriptiontext": "Learn how to messure the T8",
"adddate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"changedate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DATE_FORMAT
在 Doctrine 中本身不可用。但是,您可以编写一个自定义 dql 函数来完成这项工作,或者简单地使用现有的可用 Doctrine 扩展,例如 这个 如以下答案所示。请注意,作者在他的示例中使用了 Mysql。DATE_FORMAT
is not natively available in Doctrine. However you could write a custom dql function that does the job or simply use an existing available Doctrine extension such as this one as shown in the following answer. Be careful that the author use Mysql in his example.要获取 RFC3339 格式的日期,您需要使用序列化器组件 <代码>DateTimeNormalizer。这通常是自动加载的。
但是,通过直接实例化
JsonResponse
并使用setData
,您正在使用php 的json_encode
而不是 Symfony Serializer。使用AbstractController::json
相反,它应该按预期工作。To get a RFC3339 formatted date you need to use the Serializer component
DateTimeNormalizer
. This is usually loaded automatically.However, by instancing a
JsonResponse
directly and usingsetData
, you are using php'sjson_encode
and not the Symfony Serializer. UseAbstractController::json
instead and it should work as expected.