简体   繁体   中英

Symfony2 Data won't update

I have a entity which I'm trying to update via a form. The data gets fetched but won't persist when its submitted. Here's the controller (usually broken off outside the controller but for this testing purpose I moved it in):

/**
 * @Security("has_role('ROLE_QC')")
 * @Route("/ar20000/{articleNumber}/edit", requirements={"articleNumber": "\d+"})
 */
  public function editAction($articleNumber, Request $request) {

    $em = $this->getDoctrine()->getManager();
    $article20000information = $em->getRepository('RegenerysQMSBundle:Article20000Information')->findIndividual($articleNumber);
    if (!$article20000information) {
      throw $this->createNotFoundException(
              'No art ' . $articleNumber
      );
    }

     $form = $this->createFormBuilder($article20000information)
        ->add('articleNumber')
        ->add('description')
        ->add('manufacturer', 'entity', array('class' => 'RegenerysQMSBundle:Organisation', 'choice_label' => 'name', ))
        ->add('manufacturerCatalogueNumber')
        ->add('supplier', 'entity', array('class' => 'RegenerysQMSBundle:Organisation', 'choice_label' => 'name',))
        ->add('supplierCatalogueNumber')
        ->add('secondarySupplier','entity', array('class' => 'RegenerysQMSBundle:Organisation', 'choice_label' => 'name', 'empty_value' => 'None', 'required' => false))
        ->add('secondarySupplierCatalogueNumber')
        ->add('countedAs')
        ->add('amountInsideCounted')
        ->add('purchasedAs')
        ->add('price')
        ->add('whereToFindCertificates')
        ->add('submit', 'submit')
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $em->flush();
return $this->redirectToRoute('regenerys_qms_article20000information_individual', array('articleNumber' => $articleNumber) );
    }

    $build['form'] = $form->createView();

    return $this->render('forms/article20000Information.html.twig', $build);
 }

Here's the entity repository for findIndividual :

 public function findIndividual($articleNumber)
    {

        return $this->getEntityManager()
            ->createQuery(
                'SELECT 
    A.articleNumber,
    A.description,
    B.name as manufacturer,
    A.manufacturerCatalogueNumber,
    C.name as supplier,
    A.supplierCatalogueNumber,
    D.name as secondarySupplier,
    A.secondarySupplierCatalogueNumber,
    A.countedAs,
    A.amountInsideCounted,
    A.purchasedAs,
    A.price,
    A.whereToFindCertificates
FROM 
    RegenerysQMSBundle:Article20000Information A
    LEFT OUTER JOIN RegenerysQMSBundle:Organisation B WITH B.id = A.manufacturer 
    LEFT OUTER JOIN RegenerysQMSBundle:Organisation C WITH C.id = A.supplier 
    LEFT OUTER JOIN RegenerysQMSBundle:Organisation D WITH D.id = A.secondarySupplier
WHERE
    A.articleNumber = :articleNumber'
            )->setParameter('articleNumber', $articleNumber)
            ->getSingleResult();
    }

My twig template is very simple:

{{ form_start(form) }}

{{ form_widget(form) }}

{{ form_end(form) }}

I must be missing something obvious but I don't see it. Other similar actions I have work just fine.

I have the answer.

I compared two controllers that I thought were the same but there was one critical difference - the find method I picked. One was find($id) , the other was some custom DQL in my repository.

I can't use the $id for this one but there's a nifty method that pulls single results based on whatever you want, so in my case findOneByArticleNumber($articleNumber)

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