API 平台 :覆盖包括持久性过程在内的操作



我正在使用: + Symfony 4.4 (lts( + API 平台 2.5 (当前版本(

并且我正在尝试覆盖删除操作,以便实体不会始终被删除,我必须检查该实体是否已用作另一个实体的外键,因此在这种情况下,我不删除我只是将我的实体的标志属性设置为 false,如果没有,我将删除实体。

但是 API 平台文档中的问题,覆盖操作只是在持久化实体之前修改实体。

src/实体/产品:

/**
* @ApiResource(
*     itemOperations={
*         "get",
*         "delete"={
*             "controller"=ProductDeleteAction::class,
*         }
*     })
*/
class Product
{
//...
}

src/Controller/ProductDeleteAction

class ProductDeleteAction
{
private $manager;
public function __construct(EntityManagerInterface $manager)
{
$this->manager= $manager;
}
public function __invoke(Product $product): Product
{
$categories = $this->manager->getDoctrine()
->getRepository(Categorie::class)->findBy('product',$product);
if(count($categories)){
$product->setActive(false);
return $product;
//I want that the product been persisted But the product is deleted finallay because it's delete operation :(
}else{
return $product;
}
}
}

您必须使用数据持久化器

https://api-platform.com/docs/core/data-persisters/

最新更新