我有两个模型X和Y。对于这些模型,我还有一个服务和存储库。
结构如下:
| App
|| Http
||| Controller
|||| XController
|||| YController.php
| Service
|| XService.php
|| YService.php
| Repository
|| XRepository.php
|| YRepository.php好的。现在我有了第三个控制器Z(与模型断开)。
| App
|| Http
||| Controller
|||| XController
|||| YController.php
|||| ZController.php <!--- THIS
| Service
|| XService.php
|| YService.php
| Repository
|| XRepository.php
|| YRepository.php在ZController中,我必须通过两个服务(XService和YService)回忆创建方法。
class ZController extends Controller
{
protected XService $XService;
protected YService $YService;
public function __construct(XService $XService, YService $YService)
{
$this->XService = $XService;
$this->YService = $YService;
}
public function store(Request $request): JsonResponse
{
$x = $this->XService->create();
$y = $this->YService->create();
}
}我遇到的问题是:如果$y的服务失败(异常),我还必须删除$x的插入。如果我输入DB::transaction,当它是一个嫁接到其他事务时会生成一个错误。我可以申请什么解决方案?
而且,可以在存储库或服务中应用try catch吗?或者两种情况都是这样?
发布于 2022-11-28 08:18:17
PHP不支持多重继承,但是通过在PHP中使用接口或者在PHP中使用特性而不是类,Laravel也是如此,如果您不想通过创建存储库,那么您可以使用简单的特性来完成任务。
https://stackoverflow.com/questions/71926156
复制相似问题