要恢复软删除的元素,你可以按照以下步骤使用 ApiPlatform:
isDeleted
,用于标记是否已删除。use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class YourEntity
{
// ...
/**
* @ORM\Column(type="boolean")
*/
private $isDeleted = false;
// ...
public function getIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(bool $isDeleted): self
{
$this->isDeleted = $isDeleted;
return $this;
}
}
use App\Entity\YourEntity;
/**
* @Route("/your_entity/{id}/restore", name="your_entity_restore", methods={"PUT"})
*/
public function restoreYourEntity(YourEntity $yourEntity): JsonResponse
{
$yourEntity->setIsDeleted(false);
$this->getDoctrine()->getManager()->flush();
return $this->json('Your entity has been restored.');
}
# config/packages/api_platform.yaml
api_platform:
# ...
operations:
restore:
method: 'PUT'
path: '/your_entity/{id}/restore'
controller: App\Controller\YourEntityController::class.'::restoreYourEntity'
formats: [ 'json' ]
openapi_context:
summary: 'Restore a soft-deleted YourEntity'
description: 'Restore a soft-deleted YourEntity by setting isDeleted to false.'
parameters:
- name: 'id'
in: 'path'
required: true
schema:
type: 'string'
responses:
'200':
description: 'Your entity has been restored.'
现在,你可以向 /your_entity/{id}/restore
发送 PUT 请求来恢复软删除的元素。
下一篇:API抛出的序列不存在错误