2012-09-15 4 views
1

나는 며칠 동안이 일에 매달 렸고 온라인에서 찾을 수있는 모든 해결책을 시도했지만 운이 없었습니다.중첩 된 양식 오류 "선택 필드에 전달 된 엔티티를 관리해야합니다"

나는 많은 전학에 연결된 학생이 있습니다. 각 전송은 Classe에 연결됩니다.

양식을 만들려면 다음 클래스를 사용합니다. StudentType, TransferType.

StudentType 형식 classe에는 TransferType 유형의 필드가 있습니다. 아래 코드.

StudentType.php

<?php 
// src/PS/PSchoolBundle/Form/StudentType.php 

namespace PS\PSchoolBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use PS\PSchoolBundle\Entity\Responsible; 
use PS\PSchoolBundle\Entity\AuthorizedPerson; 
use PS\PSchoolBundle\Entity\Transfer; 

class StudentType extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('subNumber', 'text', array('required' => false)) 
      ->add('subDate', 'date', array(
         'required' => false, 
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('lastName', 'text') 
      ->add('firstName', 'text') 
      ->add('lastNameSecondLanguage', 'text', array('required' => false)) 
      ->add('firstNameSecondLanguage', 'text', array('required' => false)) 
      ->add('birthDate', 'date', array('required' => false, 
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('birthPlace', 'text', array('required' => false)) 
      ->add('birthPlaceSecondLanguage', 'text', array('required' => false)) 
      ->add('gender', 'choice', array('choices' => array('' => 'Veuillez choisir', 'm' => 'Garçon', 'f' => 'Fille'))) 
      ->add('nationality', 'text', array('required' => false)) 
      ->add('homeLanguage', 'text', array('required' => false)) 
      ->add('bloodType', 'text', array('required' => false)) 
      ->add('familySituation', 'choice', array('required' => false, 
                  'choices' => array(
                  '' => 'Veuillez choisir', 
                  'm' => 'Mariés', 
                  'd' => 'Divorcés', 
                  'w' => 'Veuve', 
                  'a' => 'Autre'))) 
      ->add('schoolStatusBefore', 'choice', array('required' => false, 'choices' => array('' => 'Veuillez choisir', 's' => 'Scolarisé', 'ns' => 'Non scolarisé'))) 
      ->add('remark', 'textarea', array('required' => false)) 
      ->add('responsibles', 'collection', array('type' => new ResponsibleType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)) 
      ->add('authorizedPersons', 'collection', array('type' => new AuthorizedPersonType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)) 

      ->add('transfers', 'collection', array('type' => new TransferType, 
                 'prototype' => true, 
                 'allow_add' => true, 
                 'allow_delete' => true)); 
    } 

    public function getName() 
    { 
     return 'student'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'PS\PSchoolBundle\Entity\Student' 
     ); 
    } 
} 

TransferType.php 내가 대신 양식을 보여, 나는이 발생이 오류를 얻을 수는 transferType 형태로 "CLASSE"필드를 추가

<?php 
// src/PS/PSchoolBundle/Form/TransferType.php 

namespace PS\PSchoolBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 
use PS\PSchoolBundle\Entity\Transfer; 
use Doctrine\ORM\EntityRepository; 

class TransferType extends AbstractType 
{ 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('reason', 'choice', array(
         'choices' => array('success' => 'Réussite', 'failure' => 'Échec'), 
         'required' => false 
         )) 
      ->add('date', 'date', array(
         'widget' => 'single_text', 
         'format' => 'dd/MM/yyyy', 
         'attr' => array('class' => 'datepicker') 
        )) 
      ->add('school', 'text', array('required' => false)) 
      ->add('classe', 'entity', array('class' => 'PSPSchoolBundle:Classe', 'property' => 'name')); 
    } 

    public function getName() 
    { 
     return 'transfer'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'PS\PSchoolBundle\Entity\Transfer' 
     ); 
    } 
} 

:

선택 필드에 전달 된 엔터티를 관리해야합니다. 500 내부 서버 오류 - FormException

전송 유형을 시도했는데 형식이 채워진 선택 입력란이 올바르게 채워지는 것을 유의하십시오. 이 문제는 TransferType과 StudentType을 함께 사용할 때만 발생합니다.

감사

편집 - 아래의 양식을 생성하는 컨트롤러의 동작은 다음과 같습니다

public function byIdEditAction($id) 
{ 
     $em = $this->getDoctrine()->getEntityManager(); 

     if(!$student = $em->getRepository('PSPSchoolBundle:Student')->find($id)) 
     { 
     throw $this->createNotFoundException('Student[id='.$id.'] does not exist.'); 
     } 

     $form  = $this->createForm(new StudentType(), $student); 
     $formHandler = new StudentHandler($form, $this->get('request'), $em); 

     if($formHandler->process()) 
     { 
      $this->get('session')->setFlash('wehappy', "L'élève a été mis à jour."); 
      return $this->redirect($this->generateUrl('_studentsbyidshow', array('id' => $student->getId()))); 
     } 

     return $this->render('PSPSchoolBundle:Student:edit.html.twig', array(
      'form' => $form->createView(), 
      'student' => $student 
    )); 
} 
+0

양식을 생성하는 컨트롤러의 코드를 게시 할 수 있습니까? –

+0

양식을 생성하는 작업을 추가하기 만하면됩니다. 당신의 도움을 주셔서 감사합니다. –

+0

코드를 살펴본 결과 모든 것이 잘된 것처럼 보입니다. 이 오류는 Classe 객체가 엔티티 관리자에 의해 관리되지 않는다고 제안하는 것 같습니다. 이 클래스는 Doctrine에서 관리한다고 가정합니다. 그렇지? 그 밖의 무엇이 잘못 될 수 있는지 확실하지 않습니다. –

답변

0

내 문제가 실제로 내가 엔티티 필드가있는 StudentType를 사용하고 있다는 사실에 연결되었다 (전송) 엔티티 필드 (Classe)가 있습니다. Student 객체에 연결된 전송 객체에는 Classe가 연결되어 있지 않으므로 클래스 선택 필드에 추가해야하는 Classe 객체가 관리되지 않았다는 사실에 대해 불만을 제기했습니다. 이는 엔티티 관리자가 관리해야 함을 의미합니다. 다른 말로하면 데이터베이스에서 오는 것이 었습니다.

이 문제를 해결하는 데는 두 가지 방법이 있습니다. Doctrine에서 해당 관계가 Nullable로 설정되어있어 데이터베이스에서 누락 된 링크를 수정하는 것이 분명했습니다. 두 번째 방법은 기본 클라스 객체를 생성하고 데이터베이스에서 당겨, 다음과 같이 전송에 할당했다 : ...

if(!$classe = $em->getRepository('PSPSchoolBundle:Classe')->find(-2)) 
    { 
     throw $this->createNotFoundException('Classe[id='.$id.'] does not exist.'); 
    } 

    $transfer->setClasse($classe); 

실제로 StudentType를 사용하여 양식을 작성하기 전에

희망이 사람을 도움이됩니다.