Browse Source

Merge pull request #2226 from pokap/improve_create_update

Improve create and update process
Thomas 11 years ago
parent
commit
ffcc989692

+ 14 - 2
Admin/Admin.php

@@ -611,12 +611,18 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             $extension->preUpdate($this, $object);
         }
 
-        $this->getModelManager()->update($object);
+        $result = $this->getModelManager()->update($object);
+        // BC compatibility
+        if (null !== $result) {
+            $object = $result;
+        }
 
         $this->postUpdate($object);
         foreach ($this->extensions as $extension) {
             $extension->postUpdate($this, $object);
         }
+
+        return $object;
     }
 
     /**
@@ -629,7 +635,11 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             $extension->prePersist($this, $object);
         }
 
-        $this->getModelManager()->create($object);
+        $result = $this->getModelManager()->create($object);
+        // BC compatibility
+        if (null !== $result) {
+            $object = $result;
+        }
 
         $this->postPersist($object);
         foreach ($this->extensions as $extension) {
@@ -637,6 +647,8 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         }
 
         $this->createObjectSecurity($object);
+
+        return $object;
     }
 
     /**

+ 2 - 2
Controller/CRUDController.php

@@ -317,7 +317,7 @@ class CRUDController extends Controller
 
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
-                $this->admin->update($object);
+                $object = $this->admin->update($object);
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array(
@@ -530,7 +530,7 @@ class CRUDController extends Controller
                     throw new AccessDeniedException();
                 }
 
-                $this->admin->create($object);
+                $object = $this->admin->create($object);
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array(

+ 2 - 2
Model/ModelManagerInterface.php

@@ -35,14 +35,14 @@ interface ModelManagerInterface
     /**
      * @param mixed $object
      *
-     * @return void
+     * @return mixed
      */
     public function create($object);
 
     /**
      * @param mixed $object
      *
-     * @return void
+     * @return mixed
      */
     public function update($object);
 

+ 16 - 0
Tests/Controller/CRUDControllerTest.php

@@ -978,6 +978,10 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->method('getObject')
             ->will($this->returnValue($object));
 
+        $this->admin->expects($this->once())
+            ->method('update')
+            ->will($this->returnArgument(0));
+
         $this->admin->expects($this->once())
             ->method('isGranted')
             ->with($this->equalTo('EDIT'))
@@ -1063,6 +1067,10 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->method('getObject')
             ->will($this->returnValue($object));
 
+        $this->admin->expects($this->once())
+            ->method('update')
+            ->will($this->returnArgument(0));
+
         $this->admin->expects($this->once())
             ->method('isGranted')
             ->with($this->equalTo('EDIT'))
@@ -1261,6 +1269,10 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->method('getNewInstance')
             ->will($this->returnValue($object));
 
+        $this->admin->expects($this->once())
+            ->method('create')
+            ->will($this->returnArgument(0));
+
         $form = $this->getMockBuilder('Symfony\Component\Form\Form')
             ->disableOriginalConstructor()
             ->getMock();
@@ -1346,6 +1358,10 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->method('getNewInstance')
             ->will($this->returnValue($object));
 
+        $this->admin->expects($this->once())
+            ->method('create')
+            ->will($this->returnArgument(0));
+
         $form = $this->getMockBuilder('Symfony\Component\Form\Form')
             ->disableOriginalConstructor()
             ->getMock();