2014-04-23 2 views
0

엔티티의 편집 양식 필드를 가져 와서 각 필드에 엔티티 값을 배치하는 데 문제가 있습니다. 내 컨트롤러에서 첫 번째 엔티티 식별자로 특정 행을 먼저 검색 할 수있었습니다. 이 경우 폼에서 DA 번호 (dano)를 검색하여 행을 표시하는 작업으로 이동합니다. 이 행에는 편집 작업으로 이동하여 편집 페이지로 이동합니다. 여기에서 양식에는 편집 할 엔터티를 표시하는 입력 필드가 있습니다.Symfony : 양식으로 엔티티 편집

Catchable Fatal Error: Argument 1 passed to 
...Bundle\Controller\EditController::createEditForm() must be an instance of 
...\Bundle\Entity\SumitomoMain, array given, called in 
...\Bundle\Controller\EditController.php on line 83 and defined in 
...\Bundle\Controller\EditController.php line 99 

은 확실히 문제를 확인하는 시작 위치를 모르는 : 모두 함께 연결하려고 할 때를 제외하고 그러나,이 오류 메시지가 표시됩니다. 내 양식 유형을 확인하고 한

/** 
* @return \Symfony\Component\HttpFoundation\Response 
* @Route("/", name="edit_home") 
* @Template() 
* @Method("GET") 
*/ 
public function indexAction(Request $request) 
{ 
    $form = $this->createDAForm(); 
    $form->handleRequest($request); 

    if($form->isValid()) { 

     return $this->redirect($this->generateUrl('edit_showda', array(
       'dano' => $form->get('dano')->getData() 
       ))); 
    } 

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

/** 
* @param Request $request 
* @return Response 
* @Route("/{dano}", name="edit_showda") 
* @Method("GET") 
* @Template() 
*/ 
public function showDAAction($dano) { 

    $getda = $this->getDoctrine() 
     ->getRepository('CIRBundle:SumitomoMain') 
     ->findByDano($dano); 
    if (!$getda) { 
     throw $this->createNotFoundException('Unable to find DA #'); 
    } 

    return $this->render('CIRBundle:Edit:showda.html.twig', array(
      'dano' => $getda 
     )); 
} 

/** 
* @param Request $request 
* @param $dano 
* @Route("/{dano}/edit", name="edit_editeda") 
* @Method("GET") 
* @Template() 
*/ 
public function editDAAction($dano) { 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('CIRBundle:SumitomoMain')->findByDano($dano); 

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

    $editform = $this->createEditForm($entity); 

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

} 

/** 
* @param Request $request 
* @param $dano 
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse 
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException 
* @Route("/{dano}", name="edit_update") 
* @Method("PUT") 
* @Template("CIRBundle:Edit:editda.html.twig") 
*/ 
public function updateDAAction(Request $request, $dano) { 
    $em = $this->getDoctrine()->getManager(); 

    $entity = $em->getRepository('CIRBundle:SumitomoMain')->find($dano); 

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

    $editForm = $this->createEditForm($entity); 
    $editForm->handleRequest($request); 

    if ($editForm->isValid()) { 
     $em->flush(); 

     return $this->redirect($this->generateUrl('edit_editeda', array('dano' => $dano))); 
    } 

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

/** 
* Creates a form to edit a SumitomoMain entity. 
* 
* @param SumitomoMain $entity The entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createEditForm(SumitomoMain $entity) 
{ 
    $form = $this->createForm(new SumitomoMainType(), $entity, array(
      'action' => $this->generateUrl('edit_update', array('dano' => $entity->getDano())), 
      'method' => 'PUT', 
     )); 

    $form->add('submit', 'submit', array('label' => 'Update')); 

    return $form; 
} 

    public function createDAForm() { 
    $form = $this->createFormBuilder() 
     ->setMethod('GET') 
     ->add('dano', 'text', array(
       'label' => 'DA #', 
      )) 
     ->add('submit','submit') 
     ->getForm(); 

    return $form; 
} 

모두가 잘 보이는 :

class SumitomoMainType extends AbstractType 
{ 
/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('dano','text', array(
       'label' => 'DA' 
      )) 
     ->add('partno','text', array(
       'label' => 'Part' 
      )) 
     ->add('batchno','text', array(
       'label' => 'Batch' 
      )) 
     ->add('indate','date', array(
       'label' => 'Date', 
       'widget' => 'single_text' 
      )) 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'CIR\Bundle\Entity\SumitomoMain' 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'cir_bundle_sumitomomain'; 
} 
} 

법인 SumitomoMain.php :

/** 
* SumitomoMain 
* 
* @ORM\Table(name="sumitomo_main", indexes={@ORM\Index(name="dano", columns={"dano"})}) 
* @ORM\Entity(repositoryClass="CIR\Bundle\Entity\SumitomoMainRepository") 
*/ 
class SumitomoMain 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer", nullable=false) 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="IDENTITY") 
*/ 
private $id; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="indate", type="date", nullable=true) 
*/ 
private $indate; 

/** 
* @var string 
* 
* @ORM\Column(name="dano", type="string", length=50, nullable=false) 
*/ 
private $dano; 

/** 
* @var string 
* 
* @ORM\Column(name="partno", type="string", length=50, nullable=false) 
*/ 
private $partno; 

/** 
* @var integer 
* 
* @ORM\Column(name="batchno", type="integer", nullable=true) 
*/ 
private $batchno; 

/** 
* @var integer 
* @ORM\OneToMany(targetEntity="SumitomoSub", mappedBy="mainId") 
*/ 
protected $sub; 

/** 
* Get id 
* 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set indate 
* 
* @param \DateTime $indate 
* @return SumitomoMain 
*/ 
public function setIndate($indate) 
{ 
    $this->indate = $indate; 

    return $this; 
} 

/** 
* Get indate 
* 
* @return \DateTime 
*/ 
public function getIndate() 
{ 
    return $this->indate; 
} 

/** 
* Set dano 
* 
* @param string $dano 
* @return SumitomoMain 
*/ 
public function setDano($dano) 
{ 
    $this->dano = $dano; 

    return $this; 
} 

/** 
* Get dano 
* 
* @return string 
*/ 
public function getDano() 
{ 
    return $this->dano; 
} 

/** 
* Set partno 
* 
* @param string $partno 
* @return SumitomoMain 
*/ 
public function setPartno($partno) 
{ 
    $this->partno = $partno; 

    return $this; 
} 

/** 
* Get partno 
* 
* @return string 
*/ 
public function getPartno() 
{ 
    return $this->partno; 
} 

/** 
* Set batchno 
* 
* @param integer $batchno 
* @return SumitomoMain 
*/ 
public function setBatchno($batchno) 
{ 
    $this->batchno = $batchno; 

    return $this; 
} 

/** 
* Get batchno 
* 
* @return integer 
*/ 
public function getBatchno() 
{ 
    return $this->batchno; 
} 



/** 
* Set sub 
* 
* @param string $sub 
* @return SumitomoMain 
*/ 
public function setSub($sub) 
{ 
    $this->sub = $sub; 

    return $this; 
} 

/** 
* Get sub 
* 
* @return string 
*/ 
public function getSub() 
{ 
    return $this->sub; 
} 
/** 
* Constructor 
*/ 
public function __construct() 
{ 
    $this->sub = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

/** 
* Add sub 
* 
* @param \CIR\Bundle\Entity\SumitomoSub $sub 
* @return SumitomoMain 
*/ 
public function addSub(\CIR\Bundle\Entity\SumitomoSub $sub) 
{ 
    $this->sub[] = $sub; 

    return $this; 
} 

/** 
* Remove sub 
* 
* @param \CIR\Bundle\Entity\SumitomoSub $sub 
*/ 
public function removeSub(\CIR\Bundle\Entity\SumitomoSub $sub) 
{ 
    $this->sub->removeElement($sub); 
} 
} 
다음

내 컨트롤러에서 작업입니다

도움이 될 것입니다!

+0

'CIRBundle : SumitomoMain' 코드를 추가 할 수 있습니까? –

+0

@Yoda가 엔티티를 추가했습니다! – Noobtastic

+0

엔티티가 아니지만 리포지토리 클래스를 사용하십시오. –

답변

1

$em->getRepository('CIRBundle:SumitomoMain')->findByDano($dano)은 일치하는 항목의 배열을 반환합니다. 그 결과를 createEditForm으로 전달합니다.이 결과는 배열이 아닌 하나의 엔티티 만 필요합니다.

결과가 하나만 나오면 대신 findOneByDano을 사용해야합니다.

+0

와우, 너 최고야! 감사! – Noobtastic

+0

'findOneByDano' 메소드가 있음을 어떻게 알 수 있습니까? 'findByDano'는 표준 API의 일부가 아니기 때문에 저의 코멘트에 저장소 클래스 코드를 물어 보았습니다. –

+0

사실 저장소에'findByDano'가 없습니다. 'findOneByDano'는 완벽하게 작동했기 때문에 둘 다 이미 프레임 워크에 있다고 추측합니다. – Noobtastic