Skip to content

Instantly share code, notes, and snippets.

@ahmetcelikezer
Created August 2, 2019 06:53
Show Gist options
  • Save ahmetcelikezer/56559b17e5474651dba0fe70e9c34113 to your computer and use it in GitHub Desktop.
Save ahmetcelikezer/56559b17e5474651dba0fe70e9c34113 to your computer and use it in GitHub Desktop.
Content controller for easy admin to create fields of empty entity.
<?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