我在使用symfony设置api平台项目和使用hautelook/alice-bundle加载固定装置时遇到了一些问题。
提供了以下实体:
// ./src/Entity/UserGroup.php
#[ApiResource(
collectionOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'userGroup:collection:get',
'item:collection:get',
],
],
],
'post' => [
'normalization_context' => [
'groups' => [
'userGroup:collection:post',
'item:collection:get',
],
],
'denormalization_context' => [
'groups' => [
'userGroup:collection:post',
],
],
],
],
itemOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'userGroup:item:get',
'item:get',
],
],
],
'put' => [
'normalization_context' => [
'groups' => [
'userGroup:item:get',
'item:get',
],
],
'denormalization_context' => [
'groups' => [
'userGroup:item:put',
],
],
]
]
)]
#[ORM\Entity]
class UserGroup
{
#[ORM\Column(type: Types::STRING, length: 128)]
#[Assert\NotBlank]
#[Assert\Length(min: 2, max: 128)]
#[Groups([
"userGroup:item:get",
"userGroup:collection:get",
"userGroup:collection:post",
"userGroup:item:put",
])]
protected string $name;
// ID, other attributes, name getter and setter, ...
}./src/Entity/User.php
#[ApiResource(
collectionOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'user:collection:get',
'item:collection:get',
],
],
],
'post' => [
'normalization_context' => [
'groups' => [
'user:collection:post',
'item:collection:get',
],
],
'denormalization_context' => [
'groups' => [
'user:collection:post',
],
],
],
],
itemOperations: [
'get' => [
'normalization_context' => [
'groups' => [
'user:item:get',
'item:get',
],
],
],
'put' => [
'normalization_context' => [
'groups' => [
'user:item:get',
'item:get',
],
],
'denormalization_context' => [
'groups' => [
'user:item:put',
],
],
]
]
)]
#[ORM\Entity]
class User
{
#[ORM\ManyToMany(targetEntity: UserGroup::class)]
#[Groups([
'userReal:item:get',
'userReal:collection:post',
'userReal:item:put'
])]
protected $userGroups;
/**
* @param mixed $userGroups
*/
public function setUserGroups($userGroups): void
{
$this->userGroups = $userGroups;
}
// ID, other attributes, name getter and setter, ...
}夹具文件包含以下数据:
App\Entity\UserGroup:
userGroup_admin:
name: 'Administrator'
active: true
App\Entity\User:
admin:
email: 'admin@domain.tld'
userGroups: [
'@userGroup_admin'
]运行命令"php /console hautelook:bin:load“将显示以下错误:
Fidry\AliceDataFixtures\Bridge\Doctrine\Persister\ObjectManagerPersister::getMetadata(): Argument #2 (
$object) must be of type object, array given, called in [...]/vendor/theofidry/alice-data-fixtures/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php o
n line 158 如果在用户类中添加"addUserGroup“和"removeUserGroup”并删除UserGroup的getter和setter,则该命令将按预期运行。这个修复之后,问题就出现在ApiPlatform请求处理中:如果一个实体有两个UserGroups,并且PUT请求(实体的完全更新)发送其他UserGroups,那么旧的UserGroups仍然存在(当然,只会导致add-方法存在,而不是完全setter)。
有什么办法解决这个问题吗?
// composer.json
{
"api-platform/core": "^2.6",
"hautelook/alice-bundle": "^2.9",
"symfony/*": "5.4.*",
}发布于 2022-01-24 19:53:50
您可以考虑修改您的User::setUserGroups()方法,如下所示:
// be sure to import the ArrayCollection
use Doctrine\Common\Collections\ArrayCollection;
/**
* @param array $userGroups
*/
public function setUserGroups(array $userGroups): void
{
$this->userGroups = new ArrayCollection($userGroups);
}在设置$userGroups时,错误可能是一个类型问题。您正在发送一个数组(在夹具中使用[] ),属性是一个ManyToMany,它通常是一个Collection。
在进行此操作之前,您应该检查代码库的其余部分,并确保它不会破坏其他内容。
https://stackoverflow.com/questions/70816118
复制相似问题