0
내가 무엇을하려고하는지 먼저 설명해 드리겠습니다.symfony2 eventlistener 유형의 컬렉션 추가하기
그래서 나는 그래서 this tutorial 및 발견 유형라는 필드를 가지고 실체라는 속성이 재산
의 값과는 PropertyValue라는 또 다른 엔티티 (는 텍스트, 이메일 또는 multi_option 될 수 있습니다) 한 내 질문은 간단한 필드 대신 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'));
}
}
}
직면 '위치를 볼 수 있습니다 그래서 기본적으로 수행하려고하고있어 내가 사용 유형의 buildForm에서 할 수있는 같은 일이다 $ 작성 도구에 매개 변수
감사는 다음 SF 2.1을 사용하는 경우
기본적으로 매개 변수가 누락되었습니다. 답변을 주셔서 감사합니다 –
당신은 빌더를 만들었고, 리스너가 연결된 폼에 빌더가 만든 폼을 추가하지 않았습니다. –