Created
August 2, 2019 06:53
-
-
Save ahmetcelikezer/56559b17e5474651dba0fe70e9c34113 to your computer and use it in GitHub Desktop.
Content controller for easy admin to create fields of empty entity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Admin; | |
use Doctrine\ORM\OptimisticLockException; | |
use Doctrine\ORM\ORMException; | |
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController; | |
class ContentController extends AdminController | |
{ | |
protected function editAction() | |
{ | |
$entity = $this->em->getRepository($this->entity['class'])->findOneBy([]); | |
if (!$entity) { | |
$entity = new $this->entity['class'](); | |
try { | |
$this->em->persist($entity); | |
$this->em->flush(); | |
} catch (OptimisticLockException | ORMException $exception) { | |
// redirect to admin's default entity when section creation failed | |
return $this->redirectToRoute('admin'); | |
} | |
} | |
$this->request->query->set('id', $entity->getId()); | |
$this->request->attributes->set( | |
'easyadmin', | |
array_merge($this->request->attributes->get('easyadmin'), ['item' => $entity]) | |
); | |
return parent::editAction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment