日期无法从 Querybuilder 格式化为字符串

发布于 2025-01-12 08:09:48 字数 1178 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2025-01-19 08:09:48

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.

亣腦蒛氧 2025-01-19 08:09:48

要获取 RFC3339 格式的日期,您需要使用序列化器组件 <代码>DateTimeNormalizer。这通常是自动加载的。

但是,通过直接实例化 JsonResponse 并使用 setData,您正在使用php 的 json_encode 而不是 Symfony Serializer。使用 AbstractController::json 相反,它应该按预期工作。

$query = $this->getDoctrine()
// Conditions ommited
;

return $this->json($query->getResult());

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 using setData, you are using php's json_encode and not the Symfony Serializer. Use AbstractController::json instead and it should work as expected.

$query = $this->getDoctrine()
// Conditions ommited
;

return $this->json($query->getResult());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文