2015-01-25 2 views
0

내 코드에서 데이터베이스에서 일부 데이터를 다운로드하여 양식에 넣어야하지만 컨트롤러 클래스 외부에서 doctrone을 얻는 방법을 모르겠습니다.Symfony2의 추상 클래스에서 doctrine을 얻는 방법은 무엇입니까?

새로운 서비스를 만들려고했지만 작동하지 않았습니다.이 경우에는 __controller()를 사용할 수 없다고 생각합니까?). 나는 또한 컨트롤러의 인스턴스를 buildForm() 메소드의 매개 변수로 전송하려고 시도했지만 메시지가 발생했습니다 : FatalErrorException: Compile Error: Declaration of MyBundle\Form\Type\TemplateType::buildForm() must be compatible with that of Symfony\Component\Form\FormTypeInterface::buildForm()).

class TemplateType extends AbstractType { 
    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
      ->add('name', 'text') 
      // ... 
      ->add('description', 'textarea'); 
    } 
    public function getName() { 
     return 'template'; 
    } 
} 

내가 buildForm 내에서 사용할 수있는 방법() 교리 :

이 내 코드? 당신이 당신의 폼 타입의 내부 컨테이너에서 서비스에 액세스하려면

public function doSomethingWithOneObjectAction($id) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('AcmeBundle:ObjectEntity')->find($id); 

    if (! $entity) { 
     throw $this->createNotFoundException('Unable to find Object entity.'); 
    } 

    $form = $this->createForm(
     new TemplateType(), 
     $entity 
    ); 

    return array(
     'entity' => $entity, 
     'form' => $form->createView() 
    ); 
} 

, 당신은 등록 먼저해야

+0

저는 스 캐 폴딩 도구와 CRUD 코드에서 사용 된 것과 동일한 원리, 특히 'Edit Object'와 관련된 부분을 사용해야한다고 생각합니다. –

+0

해결책을 찾았습니다 : 데이터베이스에서 데이터를 다운로드하기 위해 Entity Field Type을 사용했습니다. http://symfony.com/doc/2.3/reference/forms/types/entity.html – ZaquPL

답변

1

는 폼에 교리에서 데이터를 전송하기 위해, 당신은 당신의 컨트롤러에이 작업을 수행 할 필요가 그것은 서비스로서 당신에게 필요한 서비스를 주입합니다. 뭔가 같아요 this

+0

내 양식을 렌더링하기 위해 buildForm()의 데이터베이스에서 추가 데이터를 다운로드해야하기 때문에 그렇게 할 수 없습니다. – ZaquPL

+0

그런 다음 마지막 문구에서 제안 된대로 양식 유형을 서비스로 등록하고 교리 서비스를 주입해야합니다 –

관련 문제