Create a form to handle the object returned from the Doctrine Loggable extension
I want to create a custom form to manage the LogEntry object (from Loggable Behavioral ) and process the object to allow the administrator to restore some objects for older versions.
I tried the following FormType to create a form with a list of Entity versions assigned to that specific LogEntry:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Gedmo\Loggable\Entity\Repository\LogEntryRepository as EntityRepository;
class LogItemVersionsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// In that point, I would need the entity assigned to that specific LogEntry to be able to load the diferent versions
$entity = $builder->getData();
//var_dump($data);
$builder
->add('version', 'entity', array(
'class' => 'Gedmo\Loggable\Entity\LogEntry',
'property' => 'version',
'query_builder' => function(EntityRepository $er) {
return $er->getLogEntriesQuery($entity);
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Gedmo\Loggable\Entity\LogEntry'
));
}
public function getName()
{
return 'zashost_zaspanelbundle_logitemversionstype';
}
}
But I have the following doubts:
- LogEntry lacks a method to retrieve a registered object. Do I have to create an inheritance middle class LogEntry class to add this method? Note that the object assigned by the LogEntry is variable, so should I put this method in the LogEntry entity class?
+3
source to share
1 answer
What finally I did, if it could help someone:
On the form type, I set the entity to rollback as a parameter in the __construct method, so I can get the versions that this particular object should build:
class LogItemVersionsType extends AbstractType
{
protected $entity;
public function __construct($entity = null)
{
$this->entity = $entity;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entity = $this->entity;
$builder
->add('version', 'entity', array(
'mapped' => false,
'class' => 'Gedmo\Loggable\Entity\LogEntry',
'property' => 'version',
'query_builder' => function(EntityRepository $er) use ($entity) {
return $er->createQueryBuilder('le')->where('le.objectId = ?1')->setParameter('1', $entity->getId());
},
))
;
}
}
In the controller, I get the associated object and then pass the FormType to the constructor, after that in the updateAction I get the object and version again to pass the return method:
/**
* LogItem controller.
*
*/
class LogItemController extends Controller
{
/**
* Finds and displays a LogItem entity versions form.
*
*/
public function versionsAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('GedmoLoggable:LogEntry')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find HttpdHost entity.');
}
$loggedobject = $em->getRepository($entity->getObjectClass())->find($entity->getObjectId());
$editForm = $this->createForm(new LogItemVersionsType($loggedobject), $entity);
return $this->render('ZashostZaspanelBundle:LogItem:versions.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
/**
* Edits an existing LogItem entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('GedmoLoggable:LogEntry')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find LogItem entity.');
}
$refobjrepo = $em->getRepository($entity->getObjectClass());
$refobject = $refobjrepo->find($entity->getObjectId());
$editForm = $this->createForm(new LogItemVersionsType($refobject), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$repo = $em->getRepository('GedmoLoggable:LogEntry');
$values = $request->request->get('zashost_zaspanelbundle_logitemversionstype');
$version = intval($values['version']);
$repo->revert($refobject , $version);
$em->persist($refobject);
$em->flush();
return $this->redirect($this->generateUrl('logs'));
}
return $this->render('ZashostZaspanelBundle:LogItem:versions.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
}
+4
source to share