Permalink
Browse files

Se arreglo el problema de eliminar categoria que contiene productos.

  • Loading branch information...
1 parent 9c32398 commit 20294e19a959cd49db41f1f16aeae374605c5d2e @Marian0 Marian0 committed Apr 11, 2012
Showing with 19 additions and 10 deletions.
  1. +19 −10 src/HotDesign/SimpleCatalogBundle/Controller/CategoryController.php
@@ -140,7 +140,7 @@ public function updateAction($id) {
$editForm->bindRequest($request);
- if ($editForm->isValid()) {
+ if ($editForm->isValid() ) {
$em->persist($entity);
$em->flush();
@@ -167,21 +167,21 @@ public function deleteAction($id) {
$form->bindRequest($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('SimpleCatalogBundle:Category')->find($id);
+ $em = $this->getDoctrine()->getEntityManager();
+ $entity = $em->getRepository('SimpleCatalogBundle:Category')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find Category entity.');
- }
+ if (!$entity) {
+ throw $this->createNotFoundException('Unable to find Category entity.');
+ }
+ if ($form->isValid() && $this->isDeletable($entity)) {
$em->remove($entity);
$em->flush();
$this->container->get('session')->setFlash('alert-success', 'La categoría se ha eliminado con éxito.');
- } else {
- $this->container->get('session')->setFlash('alert-error', 'No se pudo eliminar la categoría.');
+ return $this->redirect($this->generateUrl('category'));
}
- return $this->redirect($this->generateUrl('category'));
+ return $this->redirect($this->generateUrl('category_edit', array('id' => $id ) ));
+
}
private function createDeleteForm($id) {
@@ -191,4 +191,13 @@ private function createDeleteForm($id) {
;
}
+ public function isDeletable(Category $categoria) {
+ $nproductos = count($categoria->getBaseEntities());
+ if ($nproductos == 0) {
+ return true;
+ }
+ $this->container->get('session')->setFlash('alert-error', 'La categoría no pudo ser eliminada debido a que contiene Items asignados. Mueva o elimine estos Items para poder eliminarla. ');
+ return false;
+ }
+
}

0 comments on commit 20294e1

Please sign in to comment.