Symfony2在类型eventlistener中添加集合

时间:2012-07-01 16:45:08

标签: symfony

首先让我解释一下我的目的是什么。

所以我有一个名为property的实体,它有一个名为type的字段(可以是text,email或multi_option)和另一个名为propertyValue的实体,它是属性的值

所以我发现this tutorial而我的问题是在EventListener而不是简单字段中如何添加一个具有其他实体值的复选框或复选框?

这里有我的EventListener代码,你可以看到我遇到问题的地方

    <?php
namespace Comehoy\AdBundle\Form\EventListener;

use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class AddValueFieldSubscriber implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    public static function getSubscribedEvents()
    {
        // Tells the dispatcher that we want to listen on the form.pre_set_data
        // event and that the preSetData method should be called.
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }

    public function preSetData(DataEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        // During form creation setData() is called with null as an argument
        // by the FormBuilder constructor. We're only concerned with when
        // setData is called with an actual Entity object in it (whether new,
        // or fetched with Doctrine). This if statement let's us skip right
        // over the null condition.
        if (null === $data) {
            return;
        }

        // check if the ProprertyValue object is "new"
        $type = $data->getI18nField()->getProperty()->getType();

        if ('multi_option' === substr($type, 0, 12)) {
            /*
             * 
             * Here is the problem since I'm kind of sure this is not the way to do this
             * 
             */
            $builder = $this->factory->createNamedBuilder('entity', 'value');
            $builder->add('value', 'entity', array(
                'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('po')
                        ->orderBy('po.value', 'ASC');
                }
            ));
        } else {
            //It's not a multi option field so we use the type directly
            $form->add($this->factory->createNamed($type, 'value'));
        }
    }
}

所以我基本上尝试做的是使用$ bulder参数在类型的buildForm中可以做同样的事情

由于

1 个答案:

答案 0 :(得分:0)

public function preSetData(DataEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    $form->add($this->factory->createNamed('type', 'name', $data->getValue(), array(
            'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('po')
                     ->orderBy('po.value', 'ASC');
            }
    )));
}

如果您使用的是SF 2.1,那么:

public function preSetData(DataEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    $form->add($this->factory->createNamed('name', 'type', $data->getValue(), array(
            'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('po')
                     ->orderBy('po.value', 'ASC');
            }
    )));
}