0

我在使用 symfony 设置 api-platform-project 和使用 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 bin/console hautelook:fixtures: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                                   

如果在 User-class 中添加“addUserGroup”和“removeUserGroup”并删除 UserGroup 的 getter 和 setter,该命令将按预期运行。在此修复后,问题出在 ApiPlatform-Request 处理中:如果一个实体有两个 UserGroups 并且一个 PUT-request(实体的完整更新)发送其他 UserGroups,旧的 UserGroups 仍然存在(当然,导致只存在 add-methods而不是完整的二传手)。

任何想法如何解决这个问题?

// composer.json 
{
"api-platform/core": "^2.6",
"hautelook/alice-bundle": "^2.9",
"symfony/*": "5.4.*",
}

```
4

1 回答 1

0

您可能会考虑将您的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. 您正在发送一个数组([]在夹具中使用)并且属性是 aManyToMany通常是 a Collection

在执行此操作之前,您应该检查代码库的其余部分,并确保这不会破坏其他内容。

于 2022-01-24T19:53:50.593 回答