Я работаю на php 7.1 и Symfony 4.2 Framework.
Я использую метод отправки ($ GenericEvent). В GenericEvent я выкидываю Exception, но в этом случае запись в базу данных должна быть остановлена.
У меня есть класс 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 в записи базы данных GenericEventдолжен быть остановлен. Он все еще пишет.
Общий класс события:
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();
}
}
Мне нужно остановить запись в базу данных после исключения.