Skip to content

Commit

Permalink
feature #1397 Display a proper error message when a database item can…
Browse files Browse the repository at this point in the history
…not be removed (javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

Display a proper error message when a database item cannot be removed

This fixes #747.

---

When you try to remove an entity which doesn't have a `cascade: remove` option, Symfony/Doctrine display the following error message in production:

![error_before](https://cloud.githubusercontent.com/assets/73419/20457012/b296a5a8-ae81-11e6-8f98-9b16808676d7.png)

Now, thanks to this new exception, we can display a human-friendly message for this common error:

![error_after](https://cloud.githubusercontent.com/assets/73419/20457018/c0fe9736-ae81-11e6-91d1-384d0497c746.png)

Commits
-------

570f6a8 Display a proper error message when a database item cannot be removed
  • Loading branch information
javiereguiluz committed Nov 26, 2016
2 parents 1f43c96 + 570f6a8 commit 28fca82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace JavierEguiluz\Bundle\EasyAdminBundle\Controller;

use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\NoEntitiesConfiguredException;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\UndefinedEntityException;
Expand Down Expand Up @@ -332,8 +334,12 @@ protected function deleteAction()

$this->executeDynamicMethod('preRemove<EntityName>Entity', array($entity));

$this->em->remove($entity);
$this->em->flush();
try {
$this->em->remove($entity);
$this->em->flush();
} catch (ForeignKeyConstraintViolationException $e) {
throw new EntityRemoveException(array('entity' => $this->entity['name']));
}

$this->dispatch(EasyAdminEvents::POST_REMOVE, array('entity' => $entity));
}
Expand Down
26 changes: 26 additions & 0 deletions Exception/EntityRemoveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JavierEguiluz\Bundle\EasyAdminBundle\Exception;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class EntityRemoveException extends BaseException
{
public function __construct(array $parameters = array())
{
$errorMessage = sprintf('You can\'t delete this "%s" item because other items depend on it in the database.', $parameters['entity']);
$proposedSolution = "Don't delete this item or change the database configuration to allow deleting it.";

parent::__construct($errorMessage, $proposedSolution, 404);
}
}

0 comments on commit 28fca82

Please sign in to comment.