0

I have selected an entity/record from the database and pass it to my form. The form loads correctly, but when I hit save it adds a new record instead of updating the existing one.

PlanController.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()));
}

routing.yml

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

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

Any help would be greatly appreciated!

2 Answers 2

2

The problem is that you not posting the id of the plan object. Hence a new one is being created when you do the getData. Add id as a hidden field or add it to your post url in the template. Actually, I don't think the hidden field will work. Probably need to just update the post url.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this worked. I simply had to keep the ID in the URL so that as it moved from form to submitted form it would keep up. Thanks!
1
if ($form->isValid()) {
        $editPlan = $form->getData();
        $em->persist($editPlan);
        $em->flush();
}

Maybe you forget to persist entities.

2 Comments

also, $editPlan = $form->getData(); is not needed as the bind function will bind data directly to the entity passed to the form.
This did not work for me. Also, you do not have to $em-persist to do the update. You can do the $em->flush() only when updating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.