构建一个表单来处理从Doctrine Loggable扩展中恢复的实体

时间:2013-02-05 13:21:50

标签: symfony doctrine

我想构建一个特殊的表单来管理LogEntry实体(来自Loggable Behavioral)并处理实体还原以允许管理员将某些实体恢复到旧版本。

我尝试了下一个FormType来生成一个表单,其中包含分配给该特定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';
    }
}

但我有疑问:

  • LogEntry没有检索已记录对象的方法。我应该创建一个继承LogEntry的中间类来添加该方法吗?请注意,分配给LogEntry的实体是可变的,所以我应该将该方法放在LogEntry实体类中吗?

1 个答案:

答案 0 :(得分:4)

最后我做了什么,如果它可以帮助某人:

在表单类型中,我将实体设置为回滚作为__construct方法中的参数,因此我可以获取此特定对象必须构建表单的版本:

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());
                },
            ))
        ;
    }
}

在控制器中我得到了关联的实体然后我传递给FormType构造函数,之后在updateAction中我再次得到对象和要传递给revert方法的版本:

/**
 * 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(),
        ));
    }


}