3
votes

I have a field that is of type: array. I am trying to save data in this field but the admin component keeps giving me a The type of the "acceptedMeasurements" attribute must be "array", "string" given. error (obviously from Symfony).

Here is the relevant code:

/**
 * @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 seems to have this correct:

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

In admin, I have tried inputting:

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

and probably a couple more but nothing works.

Can anyone help with how I am meant to save arrays?

1
For JSON data, ["test"] should work in the request body. How did you input this array? using a text field? - Iwan Wijaya
As I explained in the question, the API works using JSON. The problem is with the admin area. When the field is of type array, react-admin gives me a text field to enter the value into. Nothing I type into this field works - Wildcard27
Yea, got the same problem here as well. Can't find a fix. @Wildcard27 have you been able to fix this? - Shahriyar Imanov
@ShahriyarImanov I haven't spent much more time on it but it seems that it may be an inherited issue from react-admin. I will check when I have a little time and report back - Wildcard27
any news to this problem? - muex

1 Answers

-1
votes

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

You can override JsonLD context in your entity definition. This will give you an "array" view in the admin component for this field.

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= [];