Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28fca82

Browse files
committed
feature EasyCorp#1397 Display a proper error message when a database item cannot 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 EasyCorp#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
2 parents 1f43c96 + 570f6a8 commit 28fca82

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Controller/AdminController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace JavierEguiluz\Bundle\EasyAdminBundle\Controller;
1313

14+
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
1415
use Doctrine\ORM\EntityManager;
1516
use Doctrine\ORM\QueryBuilder;
1617
use JavierEguiluz\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
18+
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
1719
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
1820
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\NoEntitiesConfiguredException;
1921
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\UndefinedEntityException;
@@ -332,8 +334,12 @@ protected function deleteAction()
332334

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

335-
$this->em->remove($entity);
336-
$this->em->flush();
337+
try {
338+
$this->em->remove($entity);
339+
$this->em->flush();
340+
} catch (ForeignKeyConstraintViolationException $e) {
341+
throw new EntityRemoveException(array('entity' => $this->entity['name']));
342+
}
337343

338344
$this->dispatch(EasyAdminEvents::POST_REMOVE, array('entity' => $entity));
339345
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EasyAdminBundle.
5+
*
6+
* (c) Javier Eguiluz <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace JavierEguiluz\Bundle\EasyAdminBundle\Exception;
13+
14+
/**
15+
* @author Javier Eguiluz <[email protected]>
16+
*/
17+
class EntityRemoveException extends BaseException
18+
{
19+
public function __construct(array $parameters = array())
20+
{
21+
$errorMessage = sprintf('You can\'t delete this "%s" item because other items depend on it in the database.', $parameters['entity']);
22+
$proposedSolution = "Don't delete this item or change the database configuration to allow deleting it.";
23+
24+
parent::__construct($errorMessage, $proposedSolution, 404);
25+
}
26+
}

0 commit comments

Comments
 (0)