3

我想构建一个特殊的表单来管理 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 实体类中吗?
4

1 回答 1

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


}
于 2013-02-10T17:50:16.237 回答