PUT 方法 - 创建或更新记录(upserting)
PUT Method - Create or update record (upserting)
我想为我的一个端点启用 PUT
方法的更新。因此,只要记录存在,它就会被更新,如果它不存在,它就会被创建。我在 Symfony 4.2 中使用 Api-Platform。
api 平台的默认行为似乎根本不支持它 - 当我尝试使用在我得到 [= 之前未创建的数据进行 PUT
请求时14=].
我确实遵循了官方手册,recommended way of using custom operations(下面的实体代码),但仍然在点击自定义服务之前我得到了404 Response
。
有没有可能让它发挥作用?注释映射正确,日志在post.
末尾
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ORM\Entity()
* @ApiResource(
* collectionOperations={},
* itemOperations={
* "get",
* "special"={
* "method"="PUT",
* "path"="/dummys/{id}",
* "controller"=DummysController::class
* }
* }
* )
*/
class Dummy
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
请求+日志:
curl -X PUT "http://localhost/api/v1/dummys/non-existing-id" -H "accept: application/ld+json" -H "Content-Type: application/json" -d "{ \"name\": \"string\"}"
request.INFO: Matched route "api_dummys_special_item". {"route":"api_dummys_special_item","route_parameters":{"_route":"api_dummys_special_item","_controller":"App\Controller\DummyController","_format":null,"_api_resource_class":"App\Entity\Dummy","_api_item_operation_name":"special","id":"non-existing-id"},"request_uri":"http://localhost/api/v1/dummys/non-existing-id","method":"PUT"} []"
doctrine.DEBUG: SELECT (...) WHERE p0_.id = ? ["non-existing-id"] []"
request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Not Found" at /app/vendor/api-platform/core/src/EventListener/ReadListener.php line 108 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): Not Found at /app/vendor/api-platform/core/src/EventListener/ReadListener.php:108)"} []"
您是否尝试过说明如何 bypass the automatic retrieval 的文档部分?
基本添加选项_api_receive
:
/**
* @ORM\Entity()
* @ApiResource(
* collectionOperations={},
* itemOperations={
* "get",
* "special"={
* "method"="PUT",
* "path"="/dummys/{id}",
* "controller"=DummysController::class,
* "defaults"={"_api_receive"=false} <---- add this
* }
* }
* )
*/
我想为我的一个端点启用 PUT
方法的更新。因此,只要记录存在,它就会被更新,如果它不存在,它就会被创建。我在 Symfony 4.2 中使用 Api-Platform。
api 平台的默认行为似乎根本不支持它 - 当我尝试使用在我得到 [= 之前未创建的数据进行 PUT
请求时14=].
我确实遵循了官方手册,recommended way of using custom operations(下面的实体代码),但仍然在点击自定义服务之前我得到了404 Response
。
有没有可能让它发挥作用?注释映射正确,日志在post.
末尾namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ORM\Entity()
* @ApiResource(
* collectionOperations={},
* itemOperations={
* "get",
* "special"={
* "method"="PUT",
* "path"="/dummys/{id}",
* "controller"=DummysController::class
* }
* }
* )
*/
class Dummy
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
请求+日志:
curl -X PUT "http://localhost/api/v1/dummys/non-existing-id" -H "accept: application/ld+json" -H "Content-Type: application/json" -d "{ \"name\": \"string\"}"
request.INFO: Matched route "api_dummys_special_item". {"route":"api_dummys_special_item","route_parameters":{"_route":"api_dummys_special_item","_controller":"App\Controller\DummyController","_format":null,"_api_resource_class":"App\Entity\Dummy","_api_item_operation_name":"special","id":"non-existing-id"},"request_uri":"http://localhost/api/v1/dummys/non-existing-id","method":"PUT"} []"
doctrine.DEBUG: SELECT (...) WHERE p0_.id = ? ["non-existing-id"] []"
request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Not Found" at /app/vendor/api-platform/core/src/EventListener/ReadListener.php line 108 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): Not Found at /app/vendor/api-platform/core/src/EventListener/ReadListener.php:108)"} []"
您是否尝试过说明如何 bypass the automatic retrieval 的文档部分?
基本添加选项_api_receive
:
/**
* @ORM\Entity()
* @ApiResource(
* collectionOperations={},
* itemOperations={
* "get",
* "special"={
* "method"="PUT",
* "path"="/dummys/{id}",
* "controller"=DummysController::class,
* "defaults"={"_api_receive"=false} <---- add this
* }
* }
* )
*/