Symfony:使用表单编辑实体

时间:2014-04-23 21:56:54

标签: php forms entity-framework symfony

我无法获取实体的编辑表单字段以将实体值放在每个字段中。在我的控制器中,我能够搜索首先通过其第一个实体标识符搜索该特定行。在这种情况下,它是从表单中搜索DA编号(dano),该编号转到显示该行的操作。该行将有一个编辑操作,该操作将转到编辑页面,其中表单将具有显示要编辑的实体的输入字段。但是,在尝试将它们全部插入时,我不断收到此错误消息:

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);
}
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

$em->getRepository('CIRBundle:SumitomoMain')->findByDano($dano)返回匹配项的数组。您将结果传递给createEditFormfindOneByDano只需要一个实体而不是数组。

您必须使用{{1}}代替,才能获得一个结果。