0
votes

I am afraid I might have ran into some sort of XY problem...

I have an entity "Asset" with related "AssetType" entity (One AssetType can have many Asset entities)

When creating new entity with POST method, the request fails with "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type_id' cannot be null"

Data posted from react-admin (POST to /api/assets route):

{
   "data":{
      "type":"assets",
      "attributes":{
         "name":"asdf",
         "description":"LoraWAN enabled sensor"
      },
      "relationships":{
         "asset_type":{
            "data":{
               "id":"/api/asset_types/a71b47b8-b9fb-11ea-b4d5-e6b986f12daf",
               "type":"asset_types"
            }
         }
      }
   }
}

I understand that there is data lost somewhere doing deserialization of object, but cannot figure out where. Also I have identical set of entities (Gateway and Location where each Location can have multiple Gateways) and the creation of new entities work as expected...

New to Symfony & api-platform, any help appreciated.

Asset entity is set tup to be visible in api-platform:

/**
 * @ApiResource(
 *     collectionOperations={"get", "post"},
 *     itemOperations={"get", "put", "delete"},
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\AssetRepository")
 */
class Asset
{
    /**
     * @ORM\Id
     * @ORM\Column(type="uuid_binary_ordered_time", nullable=false, unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
     * @Groups({"read"})
     */
    private $uuid;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\AssetType", inversedBy="assets", cascade={"persist"})
     * @ORM\JoinColumn(name="type_id", referencedColumnName="uuid", nullable=false)
     *
     * @Groups({"read", "write"})
     */
    private $assetType;
}

AssetType entity:

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}}
 * )
 * @ORM\Entity(repositoryClass="App\Repository\AssetTypeRepository")
 */
class AssetType
{
    /**
     * @ORM\Id()
     * @ORM\Column(name="uuid", type="uuid_binary_ordered_time", nullable=false, unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
     * @Groups({"read", "write"})
     */
    private $uuid;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"read", "write"})
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Asset", mappedBy="assetType")
     */
    private $assets;

    public function __construct()
    {
        $this->assets = new ArrayCollection();
    }

    public function getUuid()
    {
        return $this->uuid;
    }

    public function setUuid($uuid): self
    {
        $this->uuid = $uuid;
        return $this;
    }

    public function getAssets(): Collection
    {
        return $this->assets;
    }

    public function addAsset(Asset $asset): self
    {
        ...
    }

    public function removeAsset(Asset $asset): self
    {
        ...
    }

1

1 Answers

0
votes

In case anyone sumbles across similar problem - the reason for missing values was property naming and its normalization.

Relationship data posted contains key "asset_type" which needs to be converted to camelCase "assetType" in react-admin's dataProvider (that's the approach I took).