【问题标题】:How to set lock mode when fetching with EntityValueResolver使用 Entity ValueResolver 获取时如何设置锁定模式
【发布时间】:2023-02-17 13:32:52
【问题描述】:

在 Symfony 应用程序中,您可以使用 EntityValueResolver 在声明控制器路由时自动获取实体。

如何为这次获取设置 PESSIMISTIC_WRITE 锁定模式?

这个非常基本的控制器成功获取了 Product 实体,但它没有应用锁。

#[Route('/product/{id}')]
public function show(#[MapEntity] Product $product): Response
{
  // use the Product!
  // ...
}

【问题讨论】:

    标签: php symfony controller locking entity


    【解决方案1】:

    没有选项可以将 EntityValueResolver 中的锁定义为函数参数。您可以在参数中使用 Request Object 并在函数内部应用锁定。

    $entity = $this->em->find(Product::class, $id);
    // use the product for some read-only code
    
    // Later, Need to update the product
    $this->em->lock($entity, LockMode::PESSIMISTIC_WRITE);
    
    $entity->setStock($entity->getStock() - 1);
    $this->em->flush();
    

    【讨论】:

      猜你喜欢
      • 2012-11-04
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多