如何使用fosrest参数转换器更新与另一个对象有关系的对象

问题描述 投票:0回答:1

我正在研究使用FOSRestBundle和Symfony 4的rest api。我的自定义参数转换器有问题。

有2个自定义参数转换器,用于创建和更新。更新参数转换器扩展了创建参数转换器。我在正确获取对象方面遇到问题。返回的对象没有ID,任何其他相关对象(manyToOne或OneToOne)也不会自动转换为所需的对象。

这是控制器

/**
 * @Post("/properties")
 * @ParamConverter("property", converter="create_entity_from_request_body")
 *
 * @param Property $property
 * @param PropertyManager $propertyManager
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 */
public function create(Property $property, PropertyManager $propertyManager)
{
    $propertyManager->createProperty($property);
    $location = $this->generateUrl('app_property_show', ['id' => $property->getId()]);

    return $this->json($property, Response::HTTP_CREATED, ['Location' => $location]);
}

/**
 * @Patch("/properties/{id}")
 * @ParamConverter("property", converter="update_entity_from_request_body",
 *     options={"mapping": {"id": "id"}})
 *
 * @param Property $property
 * @param PropertyManager $propertyManager
 * @return \Symfony\Component\HttpFoundation\JsonResponse
 */
public function update(Property $property, PropertyManager $propertyManager)
{
    $propertyManager->updateProperty($property);

    return $this->json($property);
}

这是创建参数转换器的apply方法和构造函数

 public function __construct(
    FOSRequestBodyParamConverter $requestBodyParamConverter,
    string $validationErrorsArgument = null
) {
    $this->requestBodyParamConverter = $requestBodyParamConverter;
    $this->validationErrorsArgument = $validationErrorsArgument;
}

/**
 * @param Request $request
 * @param ParamConverter $configuration
 * @return bool
 */
public function apply(Request $request, ParamConverter $configuration)
{
    $this->requestBodyParamConverter->apply($request, $configuration);

    if ($this->validationErrorsArgument) {
        $validationErrors = $request->attributes->get($this->validationErrorsArgument);

        if (count($validationErrors) > 0) {
            throw new ValidationException($validationErrors);
        }
    }
}

这是更新参数转换器,基本上扩展了创建参数的转换器

public function __construct(
    DoctrineParamConverter $doctrineParamConverter,
    FOSRequestBodyParamConverter $requestBodyParamConverter,
    string $validationErrorsArgument = null
) {
    parent::__construct($requestBodyParamConverter, $validationErrorsArgument);

    $this->doctrineParamConverter = $doctrineParamConverter;
}

/**
 * @param Request $request
 * @param ParamConverter $configuration
 * @return bool|void
 */
public function apply(Request $request, ParamConverter $configuration)
{
    //$this->doctrineParamConverter->apply($request, $configuration);

    $options = $configuration->getOptions() + [
            'deserializationContext' => [
                'object_to_populate' => $request->attributes->get($configuration->getName()),
            ],
            'validator' => [
                'groups' => ['update'],
            ]
        ];

    $configuration->setOptions($options);

    parent::apply($request, $configuration);
}

这里有主要对象的实体,它与具有OneToOne关系的Image对象有关

/**
 * @ORM\Column(type="string", length=200)
 */
private $name;

/**
 * @ORM\OneToOne(targetEntity="App\Model\Entity\Image")
 */
private $logo;

这是我作为补丁请求发送给控制器的内容((db中有一个ID为1的图像)

{
"name": "crap home",
"logo": "1"
}

这是我转储注入到控制器的属性对象时看到的内容>

object(App\Model\Entity\Property)[385]
  private 'id' => null
  private 'name' => string 'crap home' (length=10)
  private 'logo' => null

我正在研究使用FOSRestBundle和Symfony 4的rest api。我的自定义参数转换器有问题。有两个用于创建和更新目的的自定义参数转换器。 ...

symfony parameter-passing fosrestbundle
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.