0

I started symfony today. I have to add a new field in a form and save that in a table (orange hrm customization ). I created a new form field comment. I altered the target table also. When I submit the form comment is not saving. I think I have to do some configuration in symfony to save comment in database. please help me to solve the issue

EDIT: My code is

    public function execute($request) { 

        $this->form = $this->getForm();

        print "<pre>";
    print_r($this->form->getValues());
    print "</pre>";

    //Output shows [comment] => dddd

    $leaveEntitlement = $this->getLeaveEntitlement($this->form->getValues());

    print "<pre>";
    print_r($leaveEntitlement);
    print "</pre>";



    LeaveEntitlement Object
    (
    [_node:protected] => 
    [_id:protected] => Array
    (
        [id] => 1
    )

    [_data:protected] => Array
    (
        [id] => 1
        [emp_number] => 3
        [no_of_days] => 384
        [days_used] => 0.0000
        [leave_type_id] => 2
        [from_date] => 2014-01-01 00:00:00
        [to_date] => 2014-01-31 00:00:00

        [credited_date] => 2014-01-30 00:00:00
        [note] => 
        [entitlement_type] => 1
        [deleted] => 0
        [created_by_id] => 1
        [created_by_name] => Admin
    )

    [_values:protected] => Array
    (
    )


    // It is not showing [comment] => dddd
}
1
  • Please post the code relevant to the question. You can do so by editing your own question, and adding it there, in the body of the question. It will help us helping you. And please, tell us which version of symfony you're using Commented Jan 30, 2014 at 13:13

1 Answer 1

1

This magic must happen in your Controller and you have to use two things:

You've got to call $form->handleRequest($request)

You also need to persist your entity with $em->persist($entity)->flush()

Here is an example based on your code. This is the part of the controller when you handle your form:

class YourController extends Controller
{
    public function executeAction($request)
    {
        $entity = new Comment;
        $form = $this->createForm(new CommentType(), $entity);

            $form->handleRequest($request);

            if ($form->isValid())
            {
                $em = $this->getDoctrine()->getManager(); // this is where it begins
                $em->persist($entity); // it goes on
                $em->flush(); // done!

                return $this->redirect($this->generateUrl('your_next_url'));
            }

            return $this->render('YourBundle:YourDirectory:template.html.twig', array(
                'form' => $form->createView(),
            ));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.