0

让我先解释一下我要做什么。

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

所以我找到了本教程,我的问题是在 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 中做同样的事情

谢谢

4

1 回答 1

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');
            }
    )));
}
于 2012-07-02T06:01:32.707 回答