Symfony2数据不会更新

时间:2015-09-02 16:38:46

标签: php symfony doctrine

我有一个实体,我正在尝试通过表单更新。数据被提取但在提交时不会持久存在。这是控制器(通常在控制器外部断开,但出于测试目的,我将其移入):

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

以下是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();
    }

我的树枝模板非常简单:

{{ form_start(form) }}

{{ form_widget(form) }}

{{ form_end(form) }}

我必须遗漏一些明显的东西,但我没有看到它。其他类似的行动我工作得很好。

1 个答案:

答案 0 :(得分:0)

我有答案。

我比较了两个我认为相同的控制器,但有一个关键区别 - 我选择的find方法。一个是find($id),另一个是我的存储库中的一些自定义DQL。

我无法使用$id这个,但有一个漂亮的方法可以根据你想要的任何内容提取单个结果,所以在我的情况下findOneByArticleNumber($articleNumber)