简体   繁体   中英

Symfony: Editing entities with form

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!

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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