我正在使用php 7.1和Symfony 4.2框架。
我正在使用dispatch($ GenericEvent)的方法。GenenEvent我抛出异常,但在这种情况下,数据库写操作必须停止。
我有EventSubsribersInterface类
我正在使用API PLATFORM捆绑包。我可以在kernel.request,EventPriorities :: PRE_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'am trying to throw exception*/
$genericEvent = new GenericEvent($mixArray);
$this->dispatcher->dispatch($event, $genericEvent);
/*İn this code section I'am 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;
}
}
在下面的通用事件下面的部分中有一条推荐行,您可以在此处看到,当我在GenericEvent中引发Exception时,必须停止写数据库。仍在写。
通用事件类:
namespace App\EventSubscriber\SubTask;
use ApiPlatform\Core\Exception\RuntimeException;
use App\Entity\DRSA\Options;
use App\Entity\DRSA\SubTask;
use App\Entity\DRSA\SupplierCarQueues;
use App\Entity\DRSA\SupplierCars;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
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
吗?