瀏覽代碼

doctrine event subscriber

Guillermo Espinoza 8 年之前
父節點
當前提交
563ea08680
共有 1 個文件被更改,包括 20 次插入9 次删除
  1. 20 9
      EventListener/DoctrineEventSubscriber.php

+ 20 - 9
EventListener/DoctrineEventSubscriber.php

@@ -5,6 +5,7 @@ namespace WorkflowBundle\EventListener;
 use Doctrine\Common\EventSubscriber;
 use Doctrine\Common\EventSubscriber;
 use Doctrine\ORM\Event\LifecycleEventArgs;
 use Doctrine\ORM\Event\LifecycleEventArgs;
 use WorkflowBundle\Utils\DoctrineEvents;
 use WorkflowBundle\Utils\DoctrineEvents;
+use WorkflowBundle\Utils\WorkFlowEntityClasses;
 
 
 class DoctrineEventSubscriber implements EventSubscriber
 class DoctrineEventSubscriber implements EventSubscriber
 {
 {
@@ -22,7 +23,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function prePersist(LifecycleEventArgs $args)
     public function prePersist(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_PERSIST);
     }
     }
 
 
     /**
     /**
@@ -30,7 +31,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function postPersist(LifecycleEventArgs $args)
     public function postPersist(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_PERSIST);
     }
     }
 
 
     /**
     /**
@@ -38,7 +39,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function preUpdate(LifecycleEventArgs $args)
     public function preUpdate(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_UPDATE);
     }
     }
 
 
     /**
     /**
@@ -46,7 +47,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function postUpdate(LifecycleEventArgs $args)
     public function postUpdate(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_UPDATE);
     }
     }
 
 
     /**
     /**
@@ -54,7 +55,7 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function preRemove(LifecycleEventArgs $args)
     public function preRemove(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::PRE_REMOVE);
     }
     }
 
 
     /**
     /**
@@ -62,20 +63,30 @@ class DoctrineEventSubscriber implements EventSubscriber
      */
      */
     public function postRemove(LifecycleEventArgs $args)
     public function postRemove(LifecycleEventArgs $args)
     {
     {
-        $this->execute($args);
+        $this->execute($args, DoctrineEvents::POST_REMOVE);
     }
     }
 
 
     /**
     /**
      * @param LifecycleEventArgs $args
      * @param LifecycleEventArgs $args
+     * @param string $eventName
      */
      */
-    public function execute(LifecycleEventArgs $args, $eventName = 'prePersist')
+    public function execute(LifecycleEventArgs $args, $eventName = DoctrineEvents::PRE_PERSIST)
     {
     {
         $entity = $args->getEntity();
         $entity = $args->getEntity();
         $entityManager = $args->getEntityManager();
         $entityManager = $args->getEntityManager();
         $entityClass = get_class($entity);
         $entityClass = get_class($entity);
         
         
-        $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
-        $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
+        // la $entity esta dentro de las entidades con workflow
+        if (in_array($entityClass, WorkFlowEntityClasses::getConstants())) {
+            $doctrine2WorkFlowActionRepository = $entityManager->getRepository('WorkflowBundle:Doctrine2WorkFlowAction');
+            $doctrine2WorkFlowActions = $doctrine2WorkFlowActionRepository->findAllByEventAndEntityClass($eventName, $entityClass);
+            foreach ($doctrine2WorkFlowActions as $doctrine2WorkFlowAction) {
+                $actions = $doctrine2WorkFlowAction->getActions();
+                foreach ($actions as $action) {
+//                     $action->execute($entity); // ejecutar accion, template, etc
+                }
+            }
+        }
     }
     }
 
 
 }
 }