douaipi3965 2014-04-23 21:56
浏览 49
已采纳

Symfony:使用表单编辑实体

I'm having trouble getting my edit form fields of an entity to place the entity values in each fields. In my controller I was able to have a search for to first search for that one particular row by its first entity identifier. In this case it is a search for a DA number (dano) from a form, which goes to an action that displays the row. That row will have an edit action that will go to an edit page where the form will have input fields that display the entities to edit. Except however, I keep getting this error message when trying to plug it all together:

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

I definitely don't know where to begin checking the problem.

Here are the actions in my controller:

/**
 * @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;
}

I've checked my Form Type and it all looks fine:

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

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

Any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dongtang1909 2014-04-23 22:07
    关注

    $em->getRepository('CIRBundle:SumitomoMain')->findByDano($dano) returns an array of matching items. You pass that result to createEditForm, which expects only one entity and not an array.

    You have to use findOneByDano instead, to only get one result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值