2

Fieldsets에 Doctrine의 ObjectManager를 적절하게 주입하는 데 어려움을 겪고 있습니다. 나는 모든 생성자를 넘겨주고 싶지 않습니다.Fieldset에 ObjectManager를 올바르게 삽입하는 방법은 무엇입니까?

컨트롤러 :

  • 컨트롤러가 더 ObjectManager이없는 다음과 같은 경우이다.
  • 컨트롤러는 서비스를 사용합니다.

서비스 :

  • 서비스가 ObjectManagerAwareInterface를 구현합니다.
  • 서비스는 Initializer 클래스에 의해 ObjectManager를 가져옵니다.
  • 서비스는 양식을 제공합니다.

양식 :

  • 양식이 ObjectManager가 없어야합니다 - 적어도 그들이 지금까지 하나 필요하지 않습니다.
  • 폼은 각각의 필드 집합을 만듭니다.

필드 셋 :

  • 필드 셋이 ObjectManagerAwareInterface를 구현합니다. 그것은 지금처럼

나는 양식 FIELDSET에 를 통해 서비스 에서 ObjectManager 를 전달합니다. 이 방법은 매우 추악합니다.

FormElementManager를 사용하는 솔루션이 있다는 것을 알고 있습니다. 하지만 그렇게하기 위해서 FormLoter에 폼 액세스를 주어야합니다. 이 접근법은 현재보다 더 적절하지는 않습니다.

질문은 내 Forms에 전역 ServiceLocator (또는 다른 것)를 주입하지 않고 Doctrine의 ObjectManager를 내 필드 세트에 삽입하는 방법입니다. 가능하다면 양식을 간단하게 남겨두고 싶습니다.

내 응용 프로그램이 현재 구축되어 대략 방법을 보여줍니다

:

abstract class AbstractService implements ObjectManagerAwareInterface 
{ 

    private $objectManager; 

    public function getObjectManager() 
    { 
     return $this->objectManager; 
    } 

    public function setObjectManager(ObjectManager $objectManager) 
    { 
     $this->objectManager = $objectManager; 

     return $this; 
    } 

} 

class MyService extends AbstractService 
{ 

    public function doSomething() 
    { 
     $count = $this->someCalculation(); 
     $myForm = new MyForm($count, $this->getObjectManager()); 

     // ... 
    } 

} 

class MyForm extends Form 
{ 

    private $objectManager; 

    public function __construct($count, ObjectManager $objectManager) // TODO: Remove $objectManager 
    { 
     parent::__construct('myForm'); 

     $this->objectManager = $objectManager; 

     $myCollection = new Collection('myCollection'); 
     $myCollection->setCount($count); 
     $myCollection->setTargetElement(new MyFieldset($objectManager)); // TODO: ??? 
     $myCollection->setUseAsBaseFieldset(true); 
     $this->add($myCollection); 

     $submit = new Submit('submit'); 
     $this->add($submit); 
    } 
} 

abstract class AbstractFieldset extends Fieldset 
           implements ObjectManagerAwareInterface 
{ 

    private $objectManager; 

    public function getObjectManager() 
    { 
     return $this->objectManager; 
    } 

    public function setObjectManager(ObjectManager $objectManager) 
    { 
     $this->objectManager = $objectManager; 

     return $this; 
    } 

} 

class MyFieldset extends AbstractFieldset 
{ 

    public function __construct(ObjectManager $objectManager) // TODO: Remove $objectManager 
    { 
     parent::__construct('myFieldset'); 

     $this->setObjectManager($objectManager); // TODO: Use initializer 

     $this->setHydrator(new DoctrineObject($this->getObjectManager(), true)); 
     $this->setObject(new MyModel()); 

     // ... 
    } 
} 

답변

3

은 기본적 싶어 FormElementManager을 사용하는 것입니다.

// Config array 
'form_elements' => [ 
    'factories' => [ 
     'My\Cool\Fieldset' => 'My\Cool\FieldsetFactory' 
    ] 
] 

그리고는 당신이 그것을 할 수있는 가장 적절한 방법이 될 것입니다 anormal 공장 수준의

class FieldsetFactory implements FactoryInterface 
{ 
    public function createService(ServiceLocatorInterface $sl) 
    { 
     $realServiceLocator = $sl->getServiceLocator(); 
     $entityManager  = realServiceLocator->get('Doctrine\ORM\EntityManager'); 

     return new \My\Cool\Fieldset($entityManager); 
    } 
} 

있습니다.

+1

+1 필드 집합이'$ this-> add()'를 통해 폼에 추가 되었다면, FormFactory를 통해'FormElementManager'에 접근 할 수 있습니다 (서비스 로케이터를 삽입 할 필요가 없습니다) – AlexP

관련 문제