내 코드에서 데이터베이스에서 일부 데이터를 다운로드하여 양식에 넣어야하지만 컨트롤러 클래스 외부에서 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()
);
}
, 당신은 등록 먼저해야
저는 스 캐 폴딩 도구와 CRUD 코드에서 사용 된 것과 동일한 원리, 특히 'Edit Object'와 관련된 부분을 사용해야한다고 생각합니다. –
해결책을 찾았습니다 : 데이터베이스에서 데이터를 다운로드하기 위해 Entity Field Type을 사용했습니다. http://symfony.com/doc/2.3/reference/forms/types/entity.html – ZaquPL