0

我从数据库中选择了一个实体/记录并将其传递给我的表单。表单加载正确,但是当我点击保存时,它会添加一条新记录而不是更新现有记录。

计划控制器.php

public function editPlanAction($id, Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $plan = $em->getRepository('etrakOnlineOrderProcessingBundle:Plan')->find($id);

    if (!$plan && !$request->isMethod('POST')) {
        throw $this->createNotFoundException(
                'No plan found for id ' . $id
        );
    }

    $form = $this->createForm(new PlanType(), $plan);

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            $editPlan = $form->getData();
            $em->flush();
        }
    }

    return $this->render('etrakCustomerServiceBundle:Plan:edit.html.twig', array('form' => $form->createView()));
}

路由.yml

    etrak_customer_service_plan_edit:
       pattern: /plan/edit/{id}
       defaults: { _controller: etrakCustomerServiceBundle:Plan:editPlan }

计划类型.php

       namespace etrak\CustomerServiceBundle\Form\Type;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class PlanType extends AbstractType
    {
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'etrak\OnlineOrderProcessingBundle\Entity\Plan',
                'cascade_validation' => true,
            ));
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $termsConditionsArray = array("NONE" => "No Contract", "1 Year Contract" => "1 Year Contract", "2 Year Contract" => "2 Year Contract");
            $billingFrequencyArray = array("1" => "Monthly", "6" => "6 Months", "12" => "Yearly");

            // Create the form
            $builder->add('name', 'text', array('label' => 'Plan Name: ', 'required' => false));
            $builder->add('description', 'text', array('label' => 'Plan Description: ', 'required' => false));
            $builder->add('termsConditions', 'choice', array('choices' => $termsConditionsArray, 'label' => 'Terms & Conditions', 'required' => false));
            $builder->add('amount', 'text', array('label' => 'Plan Price: ', 'required' => false));
            $builder->add('affinity', 'choice', array('choices' => array('0' => 'Yes', '1' => 'No'), 'label' => 'Affinity? ', 'expanded' => true, 'required' => false));
            $builder->add('deactivationFee', 'text', array('label' => "Deactivation Fee: ", 'required' => false));
            $builder->add('recurringInMonths', 'choice', array('choices' => $billingFrequencyArray, 'label' => 'Billing Frequency: ', 'required' => false));
            $builder->add('pricingTier', 'entity', array(
                'class' => 'etrakOnlineOrderProcessingBundle:pricingTier',
                'property' => 'name',
                'label' => "Select Pricing Tier: "
            ));
            $builder->add('activeStartDate', 'datetime', array('label' => "Effective Start Date: ", 'required' => false));
            $builder->add('activeEndDate', 'datetime', array('label' => "Effective End Date: ", 'required' => false));
        }

        public function getName()
        {
            return 'plan';
        }
    }

任何帮助将不胜感激!

4

2 回答 2

2

问题是您没有发布计划对象的 ID。因此,当您执行 getData 时正在创建一个新的。将 id 添加为隐藏字段或将其添加到模板中的帖子 url。实际上,我认为隐藏字段不会起作用。可能只需要更新帖子网址。

于 2013-03-22T23:12:04.060 回答
1
if ($form->isValid()) {
        $editPlan = $form->getData();
        $em->persist($editPlan);
        $em->flush();
}

也许你忘记了持久化实体。

于 2013-03-22T21:30:28.063 回答