Sfoglia il codice sorgente

Generic ModelManager Exception that encapsulates ORM or ODM specific exceptions.

Michel Weimerskirch 13 anni fa
parent
commit
377d128bbe

+ 3 - 2
Controller/CRUDController.php

@@ -17,6 +17,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Sonata\AdminBundle\Exception\ModelManagerException;
 
 class CRUDController extends Controller
 {
@@ -169,7 +170,7 @@ class CRUDController extends Controller
         try {
             $modelManager->batchDelete($this->admin->getClass(), $query);
             $this->get('session')->setFlash('sonata_flash_success', 'flash_batch_delete_success');
-        } catch ( \PDOException $e ) {
+        } catch ( ModelManagerException $e ) {
             $this->get('session')->setFlash('sonata_flash_error', 'flash_batch_delete_error');
         }
 
@@ -198,7 +199,7 @@ class CRUDController extends Controller
             try {
                 $this->admin->delete($object);
                 $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
-            } catch ( \PDOException $e ) {
+            } catch ( ModelManagerException $e ) {
                 $this->get('session')->setFlash('sonata_flash_error', 'flash_delete_error');
             }
 

+ 17 - 0
Exception/ModelManagerException.php

@@ -0,0 +1,17 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) 2010-2011 Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Exception;
+
+class ModelManagerException extends \Exception
+{
+
+}

+ 32 - 15
Model/ORM/ModelManager.php

@@ -18,6 +18,7 @@ use Sonata\AdminBundle\Datagrid\DatagridInterface;
 use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\QueryBuilder;
+use Sonata\AdminBundle\Exception\ModelManagerException;
 
 use Symfony\Component\Form\Exception\PropertyAccessDeniedException;
 
@@ -91,20 +92,32 @@ class ModelManager implements ModelManagerInterface
 
     public function create($object)
     {
-        $this->entityManager->persist($object);
-        $this->entityManager->flush();
+        try {
+            $this->entityManager->persist($object);
+            $this->entityManager->flush();
+        } catch ( \PDOException $e ) {
+            throw new ModelManagerException();
+        }
     }
 
     public function update($object)
     {
-        $this->entityManager->persist($object);
-        $this->entityManager->flush();
+        try {
+            $this->entityManager->persist($object);
+            $this->entityManager->flush();
+        } catch ( \PDOException $e ) {
+            throw new ModelManagerException();
+        }
     }
 
     public function delete($object)
     {
-        $this->entityManager->remove($object);
-        $this->entityManager->flush();
+        try {
+            $this->entityManager->remove($object);
+            $this->entityManager->flush();
+        } catch ( \PDOException $e ) {
+            throw new ModelManagerException();
+        }
     }
 
     /**
@@ -284,18 +297,22 @@ class ModelManager implements ModelManagerInterface
      */
     public function batchDelete($class, ProxyQueryInterface $queryProxy)
     {
-        $i = 0;
-        foreach ($queryProxy->getQuery()->iterate() as $pos => $object) {
-            $this->entityManager->remove($object[0]);
+        try {
+            $i = 0;
+            foreach ($queryProxy->getQuery()->iterate() as $pos => $object) {
+                $this->entityManager->remove($object[0]);
 
-            if ((++$i % 20) == 0) {
-                $this->entityManager->flush();
-                $this->entityManager->clear();
+                if ((++$i % 20) == 0) {
+                    $this->entityManager->flush();
+                    $this->entityManager->clear();
+                }
             }
-        }
 
-        $this->entityManager->flush();
-        $this->entityManager->clear();
+            $this->entityManager->flush();
+            $this->entityManager->clear();
+        } catch ( \PDOException $e ) {
+            throw new ModelManagerException();
+        }
     }
 
     /**