Api platform, react admin – create new Entity with relation

  api-platform.com, doctrine, react-admin, symfony

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"}}
 * )
 * @ORMEntity(repositoryClass="AppRepositoryAssetRepository")
 */
class Asset
{
    /**
     * @ORMId
     * @ORMColumn(type="uuid_binary_ordered_time", nullable=false, unique=true)
     * @ORMGeneratedValue(strategy="CUSTOM")
     * @ORMCustomIdGenerator(class="RamseyUuidDoctrineUuidOrderedTimeGenerator")
     * @Groups({"read"})
     */
    private $uuid;

    /**
     * @ORMManyToOne(targetEntity="AppEntityAssetType", inversedBy="assets", cascade={"persist"})
     * @ORMJoinColumn(name="type_id", referencedColumnName="uuid", nullable=false)
     *
     * @Groups({"read", "write"})
     */
    private $assetType;
}

AssetType entity:

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}}
 * )
 * @ORMEntity(repositoryClass="AppRepositoryAssetTypeRepository")
 */
class AssetType
{
    /**
     * @ORMId()
     * @ORMColumn(name="uuid", type="uuid_binary_ordered_time", nullable=false, unique=true)
     * @ORMGeneratedValue(strategy="CUSTOM")
     * @ORMCustomIdGenerator(class="RamseyUuidDoctrineUuidOrderedTimeGenerator")
     * @Groups({"read", "write"})
     */
    private $uuid;

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

    /**
     * @ORMOneToMany(targetEntity="AppEntityAsset", 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
    {
        ...
    }

Source: Symfony Questions

LEAVE A COMMENT