2013-02-14 24 views
1

我想創建一個自定義元素,我可以預先設置實體名稱空間說 - Application\Entity\User,並輕鬆地將該元素添加到任何形式。我在注入表單元素的時候遇到了一個問題 - 我在配置文件中找到了關於form_elements這個鍵的帖子,但它不起作用 - 所以我想可能只是設置元素並將其從表單中傳遞給objectmanager。如何創建自定義表單元素,擴展主義實體選擇元素

我想做這樣的事情。

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

甚至更​​好眼前這個

$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'), 
         ), 
        ), 
       ), 
      ), 
     ); 
    } 
} 

回答

2

添加您初始invokables您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得到TE形式:

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

最後初始化方法裏面添加元素 - 而不是構造函數,因爲他永遠不會知道的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'), 
        ), 
       ), 
      ), 
     ), 
    ); 
} 

See a discussion on this setup here.

相關問題