Zend Framework 2 Элемент формы Выбор и результат из Доктрины 2
У меня проблема с выбором элемента формы в ZF2. Я создаю доктрину запроса 2 и получаю хороший список объектов результатов.
$langs = $this->getEntityManager()->getRepository('Application\Entity\Langs')->findAll();
И создайте простую форму:
class Coupon extends Form
{
protected $objectManager;
public function __construct($name = null)
{
parent::__construct('coupon');
$this->setAttribute('method', 'post');
$this
->setAttribute('method', 'post')
->setHydrator(new ClassMethodsHydrator(false))
;
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
}
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'language',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'label' => 'default.form.message',
'empty_option' => '--- choose formElementName ---',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
}
Как преобразовать результат ($langs) в массив для value_options - выбор элемента zend? Что я должен использовать для этого?
1 ответ
Я разрешаю это.
я создаю
public function getFormElementConfig()
{
return array(
'invokables' => array(
'Coupon' => 'Application\Form\Langs',
),
'initializers' => array(
'ObjectManagerInitializer' => function ($element, $formElements) {
if ($element instanceof ObjectManagerAwareInterface) {
$services = $formElements->getServiceLocator();
$entityManager = $services->get('Doctrine\ORM\EntityManager');
$element->setObjectManager($entityManager);
}
},
),
);
и добавьте init в форму:
public function init()
{
parent::init();
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'messages',
'options' => array(
'object_manager' => $this->getObjectManager(),
'target_class' => 'Application\Entity\Langs',
'property' => 'title',
),
)
);
}
Это работает:) Спасибо Сэм.