Browse Source

Add an entity getter

Michael Hoffmann 9 years ago
parent
commit
7815661deb

+ 36 - 0
src/ORM/Exception/PersistenceFailedException.php

@@ -13,6 +13,7 @@
 namespace Cake\ORM\Exception;
 
 use Cake\Core\Exception\Exception;
+use Cake\Datasource\EntityInterface;
 
 /**
  * Used when a strict save or delete fails
@@ -20,5 +21,40 @@ use Cake\Core\Exception\Exception;
 class PersistenceFailedException extends Exception
 {
 
+    /**
+     * The entity on which the persistence operation failed
+     *
+     * @var \Cake\Datasource\EntityInterface
+     */
+    protected $_entity;
+
+    /**
+     * {@inheritDoc}
+     */
     protected $_messageTemplate = 'Entity %s failure.';
+
+    /**
+     * Constructor.
+     *
+     * @param \Cake\Datasource\EntityInterface $entity The entity on which the persistence operation failed
+     * @param string|array $message Either the string of the error message, or an array of attributes
+     *   that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
+     * @param int $code The code of the error, is also the HTTP status code for the error.
+     * @param \Exception|null $previous the previous exception.
+     */
+    public function __construct(EntityInterface $entity, $message, $code = 500, $previous = null)
+    {
+        $this->_entity = $entity;
+        parent::__construct($message, $code, $previous);
+    }
+
+    /**
+     * Get the passed in entity
+     *
+     * @return \Cake\Datasource\EntityInterface
+     */
+    public function getEntity()
+    {
+        return $this->_entity;
+    }
 }

+ 2 - 2
src/ORM/Table.php

@@ -1526,7 +1526,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $saved = $this->save($entity, $options);
         if ($saved === false) {
-            throw new PersistenceFailedException(['save', $entity]);
+            throw new PersistenceFailedException($entity, ['save']);
         }
 
         return $saved;
@@ -1882,7 +1882,7 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     {
         $deleted = $this->delete($entity, $options);
         if ($deleted === false) {
-            throw new PersistenceFailedException(['delete', $entity]);
+            throw new PersistenceFailedException($entity, ['delete']);
         }
 
         return $deleted;

+ 38 - 0
tests/TestCase/ORM/TableTest.php

@@ -6316,6 +6316,25 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests that saveOrFail returns the right entity
+     *
+     * @return void
+     */
+    public function testSaveOrFailGetEntity()
+    {
+        $entity = new Entity([
+            'foo' => 'bar'
+        ]);
+        $table = TableRegistry::get('users');
+
+        try {
+            $table->saveOrFail($entity);
+        } catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
+            $this->assertSame($entity, $e->getEntity());
+        }
+    }
+
+    /**
      * Tests that deleteOrFail triggers an exception on not successful delete
      *
      * @return void
@@ -6333,6 +6352,25 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests that deleteOrFail returns the right entity
+     *
+     * @return void
+     */
+    public function testDeleteOrFailGetEntity()
+    {
+        $entity = new Entity([
+            'id' => 999
+        ]);
+        $table = TableRegistry::get('users');
+
+        try {
+            $table->deleteOrFail($entity);
+        } catch (\Cake\ORM\Exception\PersistenceFailedException $e) {
+            $this->assertSame($entity, $e->getEntity());
+        }
+    }
+
+    /**
      * Test getting the save options builder.
      *
      * @return void