Skip to content

Commit

Permalink
Add new saveOrFail and deleteOrFail method
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Feb 13, 2017
1 parent 875b454 commit c7e09a4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/ORM/Exception/PersistenceFailedException.php
@@ -0,0 +1,24 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @since 3.4.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Exception;

use Cake\Core\Exception\Exception;

/**
* Used when a strict save or delete fails
*/
class PersistenceFailedException extends Exception
{

protected $_messageTemplate = 'Entity %s failure.';
}
41 changes: 41 additions & 0 deletions src/ORM/Table.php
Expand Up @@ -33,6 +33,7 @@
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;
use Cake\ORM\Exception\MissingEntityException;
use Cake\ORM\Exception\PersistenceFailedException;
use Cake\ORM\Exception\RolledbackTransactionException;
use Cake\ORM\Rule\IsUnique;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -1511,6 +1512,26 @@ public function save(EntityInterface $entity, $options = [])
return $success;
}

/**
* Try to save an entity or throw a PersistenceFailedException if the application rules checks failed,
* the entity contains errors or the save was aborted by a callback.
*
* @param \Cake\Datasource\EntityInterface $entity the entity to be saved
* @param array|\ArrayAccess $options The options to use when saving.
* @return \Cake\Datasource\EntityInterface
* @throws \Cake\ORM\Exception\PersistenceFailedException When the entity couldn't be saved
* @see \Cake\ORM\Table::save()
*/
public function saveOrFail(EntityInterface $entity, $options = [])
{
$saved = $this->save($entity, $options);
if ($saved === false) {
throw new PersistenceFailedException(['save', $entity]);
}

return $saved;
}

/**
* Performs the actual saving of an entity based on the passed options.
*
Expand Down Expand Up @@ -1847,6 +1868,26 @@ public function delete(EntityInterface $entity, $options = [])
return $success;
}

/**
* Try to delete an entity or throw a PersistenceFailedException if the entity is new,
* has no primary key value, application rules checks failed or the delete was aborted by a callback.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to remove.
* @param array|\ArrayAccess $options The options for the delete.
* @return bool success
* @throws \Cake\ORM\Exception\PersistenceFailedException
* @see \Cake\ORM\Table::delete()
*/
public function deleteOrFail(EntityInterface $entity, $options = [])
{
$deleted = $this->delete($entity, $options);
if ($deleted === false) {
throw new PersistenceFailedException(['delete', $entity]);
}

return $deleted;
}

/**
* Perform the delete operation.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -6295,6 +6295,43 @@ public function testLoadIntoMany()
$this->assertEquals($expected, $result);
}

/**
* Tests that saveOrFail triggers an exception on not successful save
*
* @return void
* @expectedException \Cake\ORM\Exception\PersistenceFailedException
* @expectedExceptionMessage Entity save failure.
*/
public function testSaveOrFail()
{
$entity = new Entity([
'foo' => 'bar'
]);
$table = TableRegistry::get('users');

$table->saveOrFail($entity);

$row = $table->find('all')->where(['foo' => 'bar'])->toArray();
$this->assertSame([], $row->toArray());
}

/**
* Tests that deleteOrFail triggers an exception on not successful delete
*
* @return void
* @expectedException \Cake\ORM\Exception\PersistenceFailedException
* @expectedExceptionMessage Entity delete failure.
*/
public function testDeleteOrFail()
{
$entity = new Entity([
'id' => 999
]);
$table = TableRegistry::get('users');

$result = $table->deleteOrFail($entity);
}

/**
* Test getting the save options builder.
*
Expand Down

0 comments on commit c7e09a4

Please sign in to comment.