在事件订阅服务器中引发异常时停止数据库写入



我正在研究php 7.1和Symfony 4.2框架。

我正在使用调度方法($GenericEvent(.İn 通用事件我抛出异常,但在这种情况下,必须停止数据库写入。

我有事件订阅者接口类

我正在使用API平台捆绑包。我可以在kernel.request,EventPriority::P RE_READ中捕获旧数据。

class KernelViewSubscriber implements EventSubscriberInterface
{
const PUT = "Api Üzerinden Data Güncelleme İsteği Yapılmıştır. ";
const POST = "Api Üzerinden Yeni Data Eklenmiştir. ";
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var MonologApiService
*/
private $monologApiService;
/**
* @var WriteHistoryLog
*/
private $writeHistoryLog;
public function __construct(EventDispatcherInterface $dispatcher, MonologApiService $monologApiService,WriteHistoryLog $writeHistoryLog)
{
$this->dispatcher = $dispatcher;
$this->monologApiService = $monologApiService;
$this->writeHistoryLog = $writeHistoryLog;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['SystemControl', EventPriorities::PRE_VALIDATE]
];
}
public function SystemControl(GetResponseForControllerResultEvent $event, $eventKey = "")
{
$request = json_decode($event->getRequest()->getContent());
$data = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($method == 'GET') {
return;
}
if ($method == 'PUT') {
$oldData = (object)$event->getRequest()->cookies->get("old");
$eventKey = $this->putExceptions($oldData, $data ,$request);
}
if ($method == 'POST') {
$eventKey = $this->postExceptions($data,$request);
$oldData = null;
}
if (empty($eventKey)) {
return;
}
$mixArray = array("old" => $oldData, "new" => $data,"request"=>$request,"method"=>$method);
foreach ($eventKey as $event) {
/*İn this code section I'm trying to throw exception*/
$genericEvent = new GenericEvent($mixArray);
$this->dispatcher->dispatch($event, $genericEvent);
/*İn this code section I'm trying to throw exception*/
}
return;
}
/**
* @param $oldData
* @param $newData
* @param array $eventKey
* @return array
*/
public function putExceptions($oldData, $newData, $request, $eventKey = array()): array
{
if ($newData instanceof Task) {
$changeData = new HowChangeDataForTask($oldData, $newData);
$eventKey = $changeData->checkChange();
$this->setLog($oldData, $newData, 'PUT', self::PUT, 'Task');
}
if ($newData instanceof SubTask) {
$changeData = new HowChangeDataForSubTask($oldData, $newData, $request,$this->writeHistoryLog);
$eventKey = $changeData->checkChange();
$this->setLog($oldData, $newData, 'PUT', self::PUT, 'SubTask');
}
if ($newData instanceof SupplierUsers) {
$newSupplierUser = new HowChangeDataSupplierUsers($newData,$oldData, $request);
$eventKey = $newSupplierUser->checkChange();
$this->setLog($oldData, $newData, 'PUT', self::PUT, 'SupplierUser');
}
return $eventKey;
}
public function postExceptions($newData, $request, $eventKey = array()):array
{
if ($newData instanceof SupplierUsers) {
$createData = new CreateSuppleirUser($newData);
$eventKey = $createData->checkCreate();
}
if ($newData instanceof SupplierCarLocations){
$createData = new CreateSupplierCarLocation($newData);
$eventKey = $createData->checkCreate();
}
return $eventKey;
}
public function setLog($oldData, $newData, string $type, string $text, string $entity)
{
$this->monologApiService->notice($text . $entity, array(
"Entity" => $entity,
"Type" => $type,
"newData" => (array)$newData,
"oldData" => (array)$oldData
));
return;
}
}

在下面下面有一个表扬行,上面是工作通用事件,你可以看到, 当我在泛型事件中抛出异常时,必须停止数据库写入。伊特还在写作。

通用事件类:

namespace AppEventSubscriberSubTask;
use ApiPlatformCoreExceptionRuntimeException;
use AppEntityDRSAOptions;
use AppEntityDRSASubTask;
use AppEntityDRSASupplierCarQueues;
use AppEntityDRSASupplierCars;
use DoctrineORMEntityManagerInterface;
use SymfonyComponentEventDispatcherEventDispatcherInterface;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherGenericEvent;
class CreateSupplierCar implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
public function __construct(EntityManagerInterface $entityManager,EventDispatcherInterface $dispatcher)
{
$this->entityManager = $entityManager;
$this->dispatcher = $dispatcher;
}
public static function getSubscribedEvents()
{
return [
Event::Create_SupplierCar_For_SubTask => [
['setStatus', 100]
]
];
}

public function setStatus(GenericEvent $event)
{
$data = (object)$event->getSubject();
if (isset($data->request->status)){
throw new RuntimeException("Status araç durumuna göre otomatik belirlenecektir. Status göndermeyiniz.");
}
$queue = $this->entityManager->getRepository(SupplierCarQueues::class)->findBy(array("car"=>$data->new->supplierCars,"status"=>1));
if (count($queue)>=3){
throw new RuntimeException("Araç kuyruğuna 3 den fazla iş atanamaz.");
}
$newQueue = new SupplierCarQueues();
$newQueue->setCreateDate(new DateTime());
$newQueue->setSubTask($this->entityManager->getRepository(SubTask::class)->find($data->old->id));
$newQueue->setCar($this->entityManager->getRepository(SupplierCars::class)->find($data->new->supplierCars->id));
$newQueue->setStatus($this->entityManager->getRepository(Options::class)->find(1));
$subTask = $this->entityManager->getRepository(SubTask::class)->find($data->old->id);
if ($queue){
$newQueue->setPriority(100-count($queue));
$subTask->setStatus($this->entityManager->getRepository(Options::class)->find(21));
}else{
$newQueue->setPriority(100);
$subTask->setStatus($this->entityManager->getRepository(Options::class)->find(22));
}
if (empty($data->type)){
$mixArray = array("old"=>$data->old,"new"=>$subTask,"request"=>$data->request,"type"=>"Auto");
$newEvent = new GenericEvent($mixArray);
$this->dispatcher->dispatch(Event::Change_Status_For_SubTask,$newEvent);
}
$this->entityManager->persist($newQueue);
$this->entityManager->flush();
$this->entityManager->persist($subTask);
$this->entityManager->flush();
}

}

我需要在异常后停止数据库写入。

你的问题到底是什么?您的异常是否被抛出(即您是否在浏览器中看到异常消息(?

当你抛出一个异常并且你没有抓住它时,它应该冒泡到主Symfony控制器并返回一个错误。 已经写入数据库的所有内容都应该写入(它已经存在🤷(,其余的不应该。

你确定你的setStatus被执行了吗?

你的问题到底是什么?您的异常是否被抛出(即您是否在浏览器中看到异常消息(?---> 是的,我可以看到邮递员的异常

当你抛出一个异常并且你没有抓住它时,它应该冒泡到主Symfony控制器并返回一个错误。已经写入数据库的所有内容都应该写入(它已经存在🤷(,其余的不应该。 -->抛出异常没有问题。它正在工作。数据库写入必须在异常后停止。但是工作。

你确定你的设置状态被执行了吗? --> 是的,它正在执行。

在第一个"代码"窗口中,您可以看到在系统控制函数中工作的泛型事件调度程序。此泛型事件给出抛出异常。但是在抛出异常后,必须停止所有系统,并且应该停止数据库写入。有些它是如何不停止的。PUT 请求返回响应为异常,数据库已更新。数据库不应输入任何记录。它必须是返回响应是异常,数据库不会更新。通常,当我在此部分抛出异常时,它应该停止读取代码。 因此,它继续读取代码,导致不正确的数据输入。

相关内容

  • 没有找到相关文章

最新更新