【问题标题】:Api Platform property types are always STRINGApi 平台属性类型始终为 STRING
【发布时间】:2020-09-14 10:33:47
【问题描述】:

尽管我努力使用类型提示和@var int 注释,但Api Platform 对此属性的解释始终是string

考虑这个属性:


/**
 * @var int $geonameId The ID of a Geonames.org city/place/town
 *
 * @ORM\Column(type="bigint", options={"unsigned": true}, nullable=true)
 * @Assert\Type(type="int", message="Geoname id must be an integer")
 */
protected $geonameId;

public function getGeonameId(): ?int
{
    return $this->geonameId;
}

public function setGeonameId(int $geonameId = null): self
{
    $this->geonameId = $geonameId;
    return $this;
}

我从 Api Platform 得到以下架构:

geonameId  string
           nullable:  true
           The ID of a Geonames.org city/place/town

当我尝试提交 "geonameId": 123456 时,我收到以下错误:

类的“geonameId”属性的类型必须是“string”之一(给定“int”)。

我知道 @ApiProperty 注释来自定义属性等,但是当我已经使用类型提示和/或 @var 注释时,我不想为了接受整数而冗长地定义类型。

有人认为我正在做/期待的事情有什么问题吗?提前谢谢...

【问题讨论】:

  • doctrine-project.org/projects/doctrine-orm/en/2.7/reference/… - 看起来 Doctrine 将 BIGINT 映射为字符串。 bigint: Type that maps a database BIGINT to a PHP string. 不确定是否可以尝试使用整数?需要考虑的事情..
  • 感谢@Bossman,这有效,请回答而不是评论,我将标记为正确。不敢相信这种区别,这确实是有道理的,我只是从没想过这会是罪魁祸首:)

标签: symfony doctrine-orm api-platform.com


【解决方案1】:

看起来 Doctrine 将 BIGINT 映射为字符串。

参考: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/basic-mapping.html

bigint:将数据库 BIGINT 映射到 PHP 字符串的类型。

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 2019-11-27
    • 2018-03-03
    • 2021-05-03
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多