【问题标题】:Api Platform Date Filter not working with datetime fieldsApi 平台日期过滤器不适用于日期时间字段
【发布时间】:2021-08-24 12:56:58
【问题描述】:

我有一个控制实体,它有一个 dateTime 字段(我需要这个对象的日期和时间)。我还需要让我的控件进入一个时期(只是一个日期)

由于不存在DateTime过滤器,我使用的是api平台的DateFilter

这是我的@ApiFilter:

@ApiFilter(SearchFilter::class, properties={"customer" : "exact", "customer.subscriber":"exact"},
 *              DateFilter::class, properties={"dateTime":"partial"})
 * @ORM\Entity(repositoryClass=ControlRepository::class)

这里是我的实体

class Control
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"customer:read", "control:read", "subscriber:read"})
     */
    private $id;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"customer:read", "control:read", "subscriber:read"})
     */
    private $dateTime;

当我发送 GET 请求时

http://localhost:8000/api/controls?dateTime[after]=2021-05-12

我的应用程序返回 HTTP 500:这个注解需要一个代表过滤器类的值

但是 date 和 datetime 应该是兼容的。即使我在日期旁边添加 00:00:00,我的应用程序也会向我发送相同的消息以及我所有其他人的请求。

任何人都知道如何按日期过滤我的实体而不会出现此错误?

【问题讨论】:

    标签: symfony date datetime filter api-platform.com


    【解决方案1】:

    尝试像这样使用 DateFilter:

    @ApiFilter(DateFilter::class, properties={"dateTime"})
    

    也改变这个:

    /**
    * @ORM\Column(type="datetime", nullable=true)
    * @Groups({"customer:read", "control:read", "subscriber:read"})
    */
    private $dateTime;
    

    /**
    * @ORM\Column(type="datetime", nullable=true)
    * @Groups({"customer:read", "control:read", "subscriber:read"})
    * @var \DateTime
    */
    private $dateTime;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      相关资源
      最近更新 更多