浏览代码

move crud event like method calls into the Admin class

Thomas 14 年之前
父节点
当前提交
b8e75d27f9
共有 2 个文件被更改,包括 39 次插入15 次删除
  1. 34 0
      Admin/Admin.php
  2. 5 15
      Controller/CRUDController.php

+ 34 - 0
Admin/Admin.php

@@ -366,6 +366,30 @@ abstract class Admin implements AdminInterface
 
     }
 
+    public function update($object)
+    {
+        $this->preUpdate($object);
+        $this->getModelManager()->persist($object);
+        $this->getModelManager()->flush($object);
+        $this->postUpdate($object);
+    }
+
+    public function create($object)
+    {
+        $this->prePersist($object);
+        $this->getModelManager()->persist($object);
+        $this->getModelManager()->flush($object);
+        $this->postPersist($object);
+    }
+
+    public function delete($object)
+    {
+        $this->preRemove($object);
+        $this->getModelManager()->remove($object);
+        $this->getModelManager()->flush();
+        $this->postRemove($object);
+    }
+
     public function preUpdate($object)
     {
 
@@ -386,6 +410,16 @@ abstract class Admin implements AdminInterface
 
     }
 
+    public function preRemove($object)
+    {
+
+    }
+
+    public function postRemove($object)
+    {
+
+    }
+
     /**
      * build the list FieldDescription array
      *

+ 5 - 15
Controller/CRUDController.php

@@ -58,7 +58,6 @@ class CRUDController extends Controller
      */
     public function isXmlHttpRequest()
     {
-
         return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
     }
 
@@ -125,7 +124,6 @@ class CRUDController extends Controller
      */
     public function listAction()
     {
-
         return $this->render($this->admin->getListTemplate(), array(
             'datagrid'          => $this->admin->getDatagrid(),
             'list'              => $this->admin->getList(),
@@ -172,11 +170,9 @@ class CRUDController extends Controller
         if (!$object) {
             throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
         }
-        
-        $em = $this->admin->getModelManager();
-        $em->remove($object);
-        $em->flush();
-        
+
+        $this->admin->delete($object);
+
         return new RedirectResponse($this->admin->generateUrl('list'));
     }
 
@@ -203,10 +199,7 @@ class CRUDController extends Controller
             $form->bind($this->get('request'));
 
             if ($form->isValid()) {
-                $this->admin->preUpdate($object);
-                $this->admin->getModelManager()->persist($object);
-                $this->admin->getModelManager()->flush($object);
-                $this->admin->postUpdate($object);
+                $this->admin->update($object);
 
                 if ($this->isXmlHttpRequest()) {
                    return $this->renderJson(array('result' => 'ok', 'objectId' => $object->getId()));
@@ -300,10 +293,7 @@ class CRUDController extends Controller
             $form->bind($this->get('request'));
 
             if ($form->isValid()) {
-                $this->admin->prePersist($object);
-                $this->admin->getModelManager()->persist($object);
-                $this->admin->getModelManager()->flush($object);
-                $this->admin->postPersist($object);
+                $this->admin->create($object);
 
                 if ($this->isXmlHttpRequest()) {
                    return $this->renderJson(array('result' => 'ok', 'objectId' => $object->getId()));