2014-04-23 5 views
1

사용자에게 고유 한 드롭 다운을 만들고 그걸 할 수있는 방법을 찾았지만 클라이언트에서는 좋지 않습니다. 이 일Zf2 양식 userId에 따라 달라집니다

public function __construct($name = null, EntityManager $em = null, $userId = null) 
{ 

    parent::__construct($name); 
    $this->setAttribute('method', 'post'); 

    [...] 
$this->add(array(
     'name' => 'religionId', 
     'type' => 'DoctrineModule\Form\Element\ObjectSelect', 

     'options' => array(
      'object_manager' => $this->getEntityManager(), 
      'target_class' => 'Religions\Entity\Religions', 
      'property' => 'name', 
      'disable_inarray_validator' => true, 
      'by_reference' => false, 
      'is_method' => true, 
      'find_method' => array(
       'name' => 'findBy', 
       'params' => array(
        'criteria' => array('reUserId' => $userId), 
        'orderBy' => array('name' => 'ASC'), 
       ), 
      ), 


     ), 
     'attributes' => array(
      'multiple' => false, 
      'required' => false, 

     ) 
    )); 


} 

, 양식을 초기화 할 때 i는 사용자 개체를 삽입하고 선택하기 위해 클라이언트가 원하는 $this->identity()

를 사용하여 변수 reuserId를 전송되면서 여기

는 i`ve이 무슨 짓입니다 거기에서 ....

검색된 stackoverflow 및 google,하지만 아무것도 찾을 수 없습니다 ... 어떤 도움주세요? 감사!

답변

2

ZF2에있는 모든 것을 삽입하려면 ServiceManager를 사용하고 서비스 팩토리를 만들어야합니다.

따라서 항상 ServiceManager를 통해 양식을 작성해야합니다. 예를 들어 컨트롤러에서

:

$form = $this->getServiceLocator()->get('MyModule\Form\FooForm'); 

그런 다음, '주입'사용자 팩토리 클래스 또는 폐쇄를 만드는 구입해야합니다. 그렇게 말 Module.php

public function getFormElementConfig() { 
    return array(
     'factories' => array(
     'MyModule\Form\FooForm' => function($fem) { 

      $serviceManager = $fem->getServiceLocator(); 
      $entityManager = $serviceManager->get('objectmanager'); // Doctrine object manager 

      // Load the user 
      // Example is the zfcUser authentication service, however replace 
      // this with whatever you use to maintain the users id 

      $user = $serviceManager->get('zfcuser_auth_service')->getIdentity(); 

      // Inject the user entity into the form constructor 
      $form = new FooForm($user); 

      return $form; 
     }, 
    ), 
    ); 
} 

나는 양식 종속성에 대해 생각해야 할 수도 있습니다 생각합니다. 나에게있어서 은 사용자 엔티티에 대해에 의존하지 않지만 오히려 사용자의 ID는 '종교'목록을 줄이는 데이터베이스 쿼리에 사용되어야합니다. 이것은 다음 Zend\Form\Element\Select는 '정상'을 사용할 수 있습니다 의미 -

당신은 내 예제는 사용자 엔티티하는 방법을 보여줍니다 같은 방법으로 폼에 다음이 결과를 전달이 쿼리 (종교 모음)을 실행할 수 있습니다 ObjectSelect이 아닌 - ObjectManager 등을 삽입 할 필요가 없음을 의미합니다.

+0

위대한 답변! 감사! –

관련 문제