![](/img/trans.png)
[英]Getting Doctrine\Collection from select form element in zend framework 3
[英]Zend Framework 2 Form Element Select and result from Doctrine 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元素选择的数组? 我应该怎么用呢?
我解决了。
我创造
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',
),
)
);
}
它的工作原理:) Thx Sam。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.