【发布时间】:2020-06-29 18:02:34
【问题描述】:
恐怕我遇到了某种 XY 问题...
我有一个实体“资产”和相关的“资产类型”实体(一个资产类型可以有很多资产实体)
使用 POST 方法创建新实体时,请求失败并显示“SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'type_id' cannot be null”
从 react-admin 发布的数据(POST 到 /api/assets 路由):
{
"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"
}
}
}
}
}
我知道在对对象进行反序列化的地方有数据丢失,但不知道在哪里。此外,我有一组相同的实体(网关和位置,每个位置可以有多个网关),并且新实体的创建按预期工作......
Symfony 和 api 平台的新手,感谢任何帮助。
资产实体设置为在 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;
}
资产类型实体:
/**
* @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
{
...
}
【问题讨论】:
标签: symfony doctrine react-admin api-platform.com