diff --git a/Crud/Crud.php b/Crud/Crud.php index 4387ac5..70fc405 100644 --- a/Crud/Crud.php +++ b/Crud/Crud.php @@ -79,11 +79,11 @@ public function __construct( // Entity list($bundle, $entity) = $this->parseShortcutNotation($entity); - $this->_entityClass = $this->_doctrine->getEntityNamespace($bundle).'\\'.$entity; + $this->_entityClass = $this->_doctrine->getAliasNamespace($bundle).'\\'.$entity; // EntityType list($bundle, $entityType) = $this->parseShortcutNotation($entityType); - $entityTypeClass = $this->_doctrine->getEntityNamespace($bundle); + $entityTypeClass = $this->_doctrine->getAliasNamespace($bundle); $parts = explode('\\', $entityTypeClass); array_pop($parts); $this->_entityTypeClass = implode('\\', $parts).'\\Form\\'.$entityType; @@ -215,11 +215,11 @@ protected function prepareListView() $this->_datagrid->addAction(new Action($this->trans('Delete'), $this->_request->get('_route'), array_merge($this->getConfig('addToURL'), array($this->getConfig('viewVar') => 'delete')), $this->trans('Confirm delete ?'), '#icon-remove'), 'delete'); } - if ($this->_session->hasFlash('crud_success')) { - $this->_datagrid->addNotification(new Notification($this->_session->getFlash('crud_success'), 'success')); + foreach ($this->_session->getFlashBag()->get('crud_success') as $notification) { + $this->_datagrid->addNotification(new Notification($notification, 'success')); } - if ($this->_session->hasFlash('crud_error')) { - $this->_datagrid->addNotification(new Notification($this->_session->getFlash('crud_error'), 'error')); + foreach ($this->_session->getFlashBag()->get('crud_error') as $notification) { + $this->_datagrid->addNotification(new Notification($notification, 'error')); } } @@ -234,7 +234,7 @@ protected function addAction() $this->_formDatas = $this->_form->create(new $this->_entityTypeClass, new $this->_entityClass, array(), 'redirect', $options); if ($this->_formDatas instanceof Response) { - $this->_session->setFlash('crud_success', $this->trans('Creation successful')); + $this->_session->getFlashBag()->add('crud_success', $this->trans('Creation successful')); return $this->_formDatas; } return array('crud' => $this); @@ -246,7 +246,7 @@ protected function editAction($id) $this->_formDatas = $this->_form->edit(new $this->_entityTypeClass, array($this->_entity, $id), array(), 'redirect', $options); if ($this->_formDatas instanceof Response) { - $this->_session->setFlash('crud_success', $this->trans('Edition successful')); + $this->_session->getFlashBag()->add('crud_success', $this->trans('Edition successful')); return $this->_formDatas; } return array('crud' => $this); @@ -258,7 +258,7 @@ protected function deleteAction($id) $this->_formDatas = $this->_form->delete(array($this->_entity, $id), 'redirect', $options); if ($this->_formDatas instanceof Response) { - $this->_session->setFlash('crud_success', $this->trans('Delete successful')); + $this->_session->getFlashBag()->add('crud_success', $this->trans('Delete successful')); } return $this->_formDatas; } diff --git a/Service/FormService.php b/Service/FormService.php index 9722cce..5022e3f 100644 --- a/Service/FormService.php +++ b/Service/FormService.php @@ -41,39 +41,38 @@ public function create($type, $data, array $options = array(), $responseType = ' $responseOptions = array_merge($this->defaultResponseOptions, $responseOptions); $message = null; - $em = $this->_doctrine->getEntityManager(); + $em = $this->_doctrine->getManager(); $form = $this->_formFactory->create($type, $data, $options); $request = $this->_request; - if ($request->getMethod() === 'POST') { - $form->bindRequest($request); - if ($form->isValid()) { - $em->persist($data); - $em->flush(); - - switch ($responseType) { - case 'json': - $message = array( - 'success' => true, - 'message' => $responseOptions['success'] - ); - return new Response(json_encode($message), $responseOptions['status'], array('Content-type' => 'application/json')); - break; - - case 'redirect': - return new RedirectResponse($responseOptions['url'], $responseOptions['redirectStatus']); - break; - - default: - case 'html': - $message = $responseOptions['success']; - break; - } - } - else { - $message = $responseOptions['error']; + $form->handleRequest($request); + if ($form->isValid()) { + $em->persist($data); + $em->flush(); + + switch ($responseType) { + case 'json': + $message = array( + 'success' => true, + 'message' => $responseOptions['success'] + ); + return new Response(json_encode($message), $responseOptions['status'], array('Content-type' => 'application/json')); + break; + + case 'redirect': + return new RedirectResponse($responseOptions['url'], $responseOptions['redirectStatus']); + break; + + default: + case 'html': + $message = $responseOptions['success']; + break; } } + else { + $message = $responseOptions['error']; + } + return array('form' => $form->createView(), 'item' => $data, 'action' => 'create', 'message' => $message); } @@ -82,7 +81,7 @@ public function edit($type, $data, array $options = array(), $responseType = 'ht $responseOptions = array_merge($this->defaultResponseOptions, $responseOptions); $message = null; - $em = $this->_doctrine->getEntityManager(); + $em = $this->_doctrine->getManager(); if (is_array($data)) { $data = $em->getRepository($data[0])->find($data[1]); } @@ -108,34 +107,32 @@ public function edit($type, $data, array $options = array(), $responseType = 'ht $form = $this->_formFactory->create($type, $data, $options); $request = $this->_request; - if ($request->getMethod() === 'POST') { - $form->bindRequest($request); - if ($form->isValid()) { - $em->flush(); - - switch ($responseType) { - case 'json': - $message = array( - 'success' => true, - 'message' => $responseOptions['success'] - ); - return new Response(json_encode($message), 200, array('Content-type' => 'application/json')); - break; - - case 'redirect': - return new RedirectResponse($responseOptions['url'], $responseOptions['redirectStatus']); - break; - - default: - case 'html': - $message = $responseOptions['success']; - break; - } - } - else { - $message = $responseOptions['error']; + $form->handleRequest($request); + if ($form->isValid()) { + $em->flush(); + + switch ($responseType) { + case 'json': + $message = array( + 'success' => true, + 'message' => $responseOptions['success'] + ); + return new Response(json_encode($message), 200, array('Content-type' => 'application/json')); + break; + + case 'redirect': + return new RedirectResponse($responseOptions['url'], $responseOptions['redirectStatus']); + break; + + default: + case 'html': + $message = $responseOptions['success']; + break; } } + else { + $message = $responseOptions['error']; + } return array('form' => $form->createView(), 'item' => $data, 'action' => 'edit', 'message' => $message); } @@ -145,7 +142,7 @@ public function delete($data, $responseType = 'json', array $responseOptions = a $responseOptions = array_merge($this->defaultResponseOptions, $responseOptions); $message = null; - $em = $this->_doctrine->getEntityManager(); + $em = $this->_doctrine->getManager(); if (is_array($data)) { $data = $em->getRepository($data[0])->find($data[1]); } diff --git a/composer.json b/composer.json index 33e82cc..bbf040a 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "symfony/framework-bundle": "2.*" + "symfony/framework-bundle": "~2.3" }, "autoload": { "psr-0": {