0

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 要素選択の配列に変換するにはどうすればよいですか? これには何を使用すればよいですか?

4

1 に答える 1

0

私はそれを解決します。

私は作成します

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。

于 2013-11-07T09:21:54.217 に答える