0
votes

I have an entity with one TEXT (MySQL) attributes

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Index;
use ApiPlatform\Core\Annotation\ApiProperty;


/**
 * @ApiResource(
 *     attributes={},
 *     collectionOperations={
 *         "get"={},
 *         "post"={
 *              "access_control"="is_granted('ROLE_COMPANY')"
 *           },
 *     },
 *     itemOperations={
 *         "get"={},
 *         "put"={"access_control"="is_granted('ROLE_COMPANY')"},
 *      }
 * )
 * @ORM\Entity(
 *     repositoryClass="App\Repository\SettingRepository",
 *     )
 * @ORM\Table(
 *     indexes={@Index(name="domain_idx", columns={"domain"})}
 * )
 */
class Setting
{
    /**
     * @var Uuid
     * @ApiProperty(identifier=true)
     * @ORM\Id
     * @ORM\Column(type="string")
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $identifier;

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


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



    public function getData()
    {
        if($this->data == null) return array();
        $data = unserialize($this->data);

        return $data;
    }

    public function setData($data): self
    {
        $this->data = serialize($data);
        return $this;
    }

    /**
     * @return mixed
     */
    public function getIdentifier()
    {
        return $this->identifier;
    }

    /**
     * @param mixed $key
     */
    public function setIdentifier($identifier): self
    {
        $this->identifier = $identifier;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getDomain()
    {
        return $this->domain;
    }

    /**
     * @param mixed $domain
     */
    public function setDomain($domain): self
    {
        $this->domain = $domain;
        return $this;
    }



}

If I try to invoke the service with the following parameter structure it works fine:

{
  "data": "testData",
  "identifier": "testIdentifier",
  "domain": "domain1"
}

But If I would like to store an embedded JSON string, for example: "data": {"temp": 123}

I receive the following error: hydra:description": "The type of the \"data\" attribute must be \"string\", \"array\" given.",

I tried to convert the object into an string in the method setData. But this method will not be invoked. It seams, that the API-Platform detects the wrong type and throws the exception.

I found some comments, that it is necessary to decorate the property: https://api-platform.com/docs/core/serialization/#decorating-a-serializer-and-adding-extra-data

Can anyone give me an example? It does not work! Where is the right place to serialise and unserialise the property data?

Does anyone have an idea? Kind regards

1

1 Answers

2
votes

You need to set the column type to json in MySQL. It should behave as expected.

/**
 * @var array Additional data describing the setting. 
 * @ORM\Column(type="json", nullable=true)
 */
    private $data = null;

I think null is more consistent than an empty array, but that's your choice.