1

I am trying to create a custom element where I can pre-set an entity namespace say -- Application\Entity\User and easily add that element to any form. I have an issue with injecting the doctrine em in form elements -- I found this post about the form_elements key in configuration but it doesnt work -- so I figured maybe just setup the element and pass it the objectmanager from the form.

I want to do something like this.

$this->add(
    array(
        'name' => 'user_id',
        'type' => 'Application\Form\Element\UserSelect',
        'options' => array(
            'label'          => 'User',
            'object_manager' => $this->getObjectManager(),
        ),
    ),
);

Or even better just this

$this->add(
    array(
        'name'  => 'user_id',
        'type'  => 'Application\Form\Element\UserSelect',
        'label' => 'User',
    ),
);

module.config.php

'service_manager' => array(
    'aliases' => array(
        'doctrine_service' => 'doctrine.entitymanager.orm_default',
    ),
    'initializers' => array(
        function ($instance, $sm) {
            if ($instance instanceof DoctrineModule\Persistence\ObjectManagerAwareInterface) {
                $instance->setObjectManager(
                    $sm->get('doctrine_service')
                );
            }
            if ($instance instanceof Application\Form\AbstractForm) {
                $instance->init();
            }
        },
    ),
),

AbstractForm.php

namespace Application\Form;

use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Zend\Form\Form as ZendForm;

abstract class AbstractForm extends ZendForm implements ObjectManagerAwareInterface
{
    protected $objectManager;
    public function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }
    public function getObjectManager()
    {
        return $this->objectManager;
    }
}

TestForm.php

namespace Users\Form;

use Application\Form\AbstractForm;

class Login extends AbstractForm
{
    public function init()
    {
        $this->add(
            array(
                'name' => 'user_id',
                'type' => 'DoctrineORMModule\Form\Element\DoctrineEntity',
                'options' => array(
                    'label'          => 'User',
                    'object_manager' => $this->getObjectManager(),
                    'target_class'   => 'Application\Entity\User',
                    'property'       => 'name',
                    'find_method' => array(
                        'name'   => 'findBy',
                        'params' => array(
                            'criteria' => array('is_deleted' => 0),
                            'orderBy'  => array('name' => 'ASC'),
                        ),
                    ),
                ),
            ),
        );
    }
}
4

1 回答 1

2

初始化程序可调用对象添加到Module.php的getFormElementConfig方法中:

use DoctrineModule\Persistence\ObjectManagerAwareInterface;
...
public function getFormElementConfig()
{
    return array(
    'invokables' => array(
    'MyForm' => 'Application\Form\MyForm',
        ),
        'initializers' => array(
            'ObjectManagerInitializer' => function ($element, $formElements) {
                if ($element instanceof ObjectManagerAwareInterface) {
                    $services      = $formElements->getServiceLocator();
                    $entityManager = $services->get('Doctrine\ORM\EntityManager');

                    $element->setObjectManager($entityManager);
                }
            },
        ),
    );
}

然后使用FormElementManager获取表单:

$forms = $this->getServiceLocator()->get('FormElementManager');
$myForm = $forms->get('MyForm');

最后在init方法中添加你的元素- 而不是构造函数,因为他永远不会知道objectManager

public function init()
{
    $this->add(
        array(
            'name' => 'user_id',
            'type' => 'DoctrineORMModule\Form\Element\DoctrineEntity',
            'options' => array(
                'label'          => 'User',
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Application\Entity\User',
                'property'       => 'name',
                'find_method' => array(
                    'name'   => 'findBy',
                    'params' => array(
                        'criteria' => array('is_deleted' => 0),
                        'orderBy'  => array('name' => 'ASC'),
                    ),
                ),
            ),
        ),
    );
}

在此处查看有关此设置的讨论。

于 2013-03-19T08:00:41.040 回答