【问题标题】:Saving as array in API Platform admin在 API 平台管理员中另存为数组
【发布时间】:2019-03-15 04:06:21
【问题描述】:

我有一个类型为:array 的字段。我正在尝试在此字段中保存数据,但管理组件不断给我一个 The type of the "acceptedMeasurements" attribute must be "array", "string" given. 错误(显然来自 Symfony)。

以下是相关代码:

/**
 * @ORM\Column(type="array", nullable=true)
 */
private $acceptedMeasurements;


/**
 * @return null|array
 */
public function getAcceptedMeasurements(): ?array
{
    return $this->acceptedMeasurements;
}

/**
 * @param null|array $acceptedMeasurements
 *
 * @return Exercise
 */
public function setAcceptedMeasurements(?array $acceptedMeasurements): self
{
    $this->acceptedMeasurements = $acceptedMeasurements;

    return $this;
}

Swagger 似乎是正确的:

{
  "active": true,
  "name": "string",
  "user": "string",
  "acceptedMeasurements": [
    "string"
  ]
}

在管理员中,我尝试过输入:

[test]
['test']
["test"]
{"test"}
a:1:{i:0;s:4:"test";}

可能还有更多,但没有任何效果。

谁能帮助我如何保存数组?

【问题讨论】:

  • 对于 JSON 数据,["test"] 应该在请求正文中起作用。你是怎么输入这个数组的?使用文本字段?
  • 正如我在问题中解释的那样,API 使用 JSON 工作。问题出在管理区域。当字段类型为array 时,react-admin 会给我一个文本字段来输入值。我在此字段中输入的内容均无效
  • 是的,这里也遇到了同样的问题。找不到解决办法。 @Wildcard27 你能解决这个问题吗?
  • @ShahriyarImanov 我没有花太多时间在这上面,但似乎它可能是来自react-admin 的继承问题。我会检查一下,当我有一点时间并报告回来
  • 有关于这个问题的消息吗?

标签: react-admin api-platform.com


【解决方案1】:

来自https://github.com/api-platform/admin/issues/179:

您可以在实体定义中覆盖 JsonLD 上下文。这将在该字段的管理组件中为您提供一个“数组”视图。

use ApiPlatform\Core\Annotation\ApiProperty;
...

    /**
     * @ORM\Column(type="simple_array")
     * @ApiProperty(
     *     attributes={
     *         "jsonld_context"={
     *             "@type"="http://www.w3.org/2001/XMLSchema#array",
     *         }
     *     }
     * )
     */
    private $my_simple_array_field= [];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多