Question

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
}
Was it helpful?

Solution

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(),
            ));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top