API 平台,在 MongoDB 中发布具有关系的对象
API Platform, Posting an object with a relation in MongoDB
我正在使用 API 平台和 MongoDB 开发 API。
我的资源非常简单,我有一个 User 实体,它与一个名为 UserCompany 的实体存在一对多关系。
用户集合配置如下:
/**
* @ApiResource
*
* @ODM\Document
*/
class User
{
/**
* @ODM\Id(strategy="UUID", type="string")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
private $name;
/**
* @ODM\Field(type="int")
* @Assert\NotBlank
*/
private $role = 0;
/**
* @ODM\ReferenceMany(targetDocument="UserCompany", mappedBy="user")
*/
private $companies;
...
UserCompany 集合配置如下:
/**
* @ApiResource
*
* @ODM\Document
*/
class UserCompany
{
/**
* @ODM\Id(strategy="UUID", type="string")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
private $companyId;
/**
* @ODM\Field(type="int")
* @Assert\NotBlank
*/
private $companyRole = 0;
/**
* @ODM\ReferenceOne(targetDocument="User")
* @Assert\NotBlank
*/
private $user;
测试 API,我设法 Post 一个用户:
{
"name": "whatever",
"role": 0
}
响应是
{
"@context": "/api/contexts/User",
"@id": "/api/users",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/api/users/ed34add1f16b3484b72442d7ba8b9fcb",
"@type": "User",
"id": "ed34add1f16b3484b72442d7ba8b9fcb",
"name": "whatever",
"role": 0
}
],
"hydra:totalItems": 1
}
我只是没能Post一个用户公司:
{
"companyId": "acompany",
"companyRole": 0,
"user": "ed34add1f16b3484b72442d7ba8b9fcb"
}
returns 给我一个错误:
"Could not denormalize object of type User, no supporting normalizer found."
所以显然我做错了什么,传递 ID 不是执行此操作的标准方法。我该怎么做?
好的,我找到了解决方案。上面的代码有2个错误。
1、ReferenceOne注解错误,应该是:
* @ODM\ReferenceOne(targetDocument=User::class)
而不是
* @ODM\ReferenceOne(targetDocument="User")
在 post 中使用的第 2 个用户参数是
{
"companyId": "acompany",
"companyRole": 0,
"user": "/api/users/ed34add1f16b3484b72442d7ba8b9fcb"
}
我正在使用 API 平台和 MongoDB 开发 API。 我的资源非常简单,我有一个 User 实体,它与一个名为 UserCompany 的实体存在一对多关系。
用户集合配置如下:
/**
* @ApiResource
*
* @ODM\Document
*/
class User
{
/**
* @ODM\Id(strategy="UUID", type="string")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
private $name;
/**
* @ODM\Field(type="int")
* @Assert\NotBlank
*/
private $role = 0;
/**
* @ODM\ReferenceMany(targetDocument="UserCompany", mappedBy="user")
*/
private $companies;
...
UserCompany 集合配置如下:
/**
* @ApiResource
*
* @ODM\Document
*/
class UserCompany
{
/**
* @ODM\Id(strategy="UUID", type="string")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
private $companyId;
/**
* @ODM\Field(type="int")
* @Assert\NotBlank
*/
private $companyRole = 0;
/**
* @ODM\ReferenceOne(targetDocument="User")
* @Assert\NotBlank
*/
private $user;
测试 API,我设法 Post 一个用户:
{
"name": "whatever",
"role": 0
}
响应是
{
"@context": "/api/contexts/User",
"@id": "/api/users",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/api/users/ed34add1f16b3484b72442d7ba8b9fcb",
"@type": "User",
"id": "ed34add1f16b3484b72442d7ba8b9fcb",
"name": "whatever",
"role": 0
}
],
"hydra:totalItems": 1
}
我只是没能Post一个用户公司:
{
"companyId": "acompany",
"companyRole": 0,
"user": "ed34add1f16b3484b72442d7ba8b9fcb"
}
returns 给我一个错误: "Could not denormalize object of type User, no supporting normalizer found."
所以显然我做错了什么,传递 ID 不是执行此操作的标准方法。我该怎么做?
好的,我找到了解决方案。上面的代码有2个错误。
1、ReferenceOne注解错误,应该是:
* @ODM\ReferenceOne(targetDocument=User::class)
而不是
* @ODM\ReferenceOne(targetDocument="User")
在 post 中使用的第 2 个用户参数是
{
"companyId": "acompany",
"companyRole": 0,
"user": "/api/users/ed34add1f16b3484b72442d7ba8b9fcb"
}