繁体   English   中英

Symfony:使用表单编辑实体

[英]Symfony: Editing entities with form

我在获取实体的编辑表单字段以将实体值放置在每个字段中时遇到麻烦。 在我的控制器中,我能够通过第一个实体标识符搜索到该特定行。 在这种情况下,这是从表单中搜索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);
}
}

任何帮助,将不胜感激!

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

您必须改用findOneByDano才能获得一个结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM