0

I have the next form type in my symfony2.4 project, I use doctrine ORM

<?php 
namespace TFS\RiseBundle\Form\Type;

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

class JobType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {   
        $builder->add('program', 'entity', array(
            'class'     =>  'TFSMaisBundle:DbasewuProgram',
            'empty_value'   =>  '',
        ));               

        $builder->add('department', 'entity', array(
                    'class'     =>  'TFSMaisBundle:DbasewuDepartment',
                    'empty_value'   =>  ''
                ))                        
                ->add('position', 'entity', array(
                    'class'     =>  'TFSMaisBundle:DbaseDesignation',
                    'empty_value'   =>  '',
                ))
                ->add('grade')
                ->add('hiringType', 'choice', array(
                    'expanded'  =>  true,
                    'choices'   => array(
                        0   =>  'External',
                        1   =>  'Internal',
                    ),
                    'required'  =>  true,
                ))
                ->add('neededBy')
                ->add('confirmationDate')
                ->add('filedBy')
                ->add('secondSupervisor')
                ->add('thirdSupervisor')
                ->add('createdBy')
                ->add('expirationDate')
                ->add('createdAt')
                ->add('requirement')
                ->add('jobDescription');

        $builder->add('Create', 'submit');


    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TFS\RiseBundle\Entity\Job'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'job';
    }
}

And this is the Controller:

<?php
namespace TFS\RiseBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use TFS\RiseBundle\Entity\Job;
use TFS\RiseBundle\Form\Type\JobType;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('TFSRiseBundle:Job')->findAll();

        return $this->render('TFSRiseBundle:Default:index.html.twig', array(
            'entities' => $entities,
        ));
    }

    public function newAction(Request $request)                       
    {     
        $job = new Job();
        $form = $this->createForm(new JobType(), $job);

        $form->handleRequest($request);

        if($form->isValid()){            
            $em = $this->getDoctrine()->getManager();
            $em->persist($job);
            $em->flush();
            return $this->redirect($this->generateUrl('job'));

        }

        return $this->render('TFSRiseBundle:Default:new.html.twig', array(
            'entity' => $job,
            'form'   => $form->createView(),
        ));

    }        

}

My problem is when i try to insert the value of the select fields: program, department and position because the form send values 0's to the database, with the another fields I don't have problems.

The selects fields in the form are filled by a bundle (MaisBundle) and must be inserted in another bundle (RiseBundle)

I appreciate any help.

Thanks.

I found a solution, now newAction(Request $request) in the Controller is like:

    public function newAction(Request $request)                       
    {     
        $job = new Job();
        $form = $this->createForm(new JobType(), $job);        
        $form->handleRequest($request);

        if($form->isValid())
        {                                    
            //get all the variables in request
            foreach ($request->request->all() as $req) {}

            $em = $this->getDoctrine()->getManager();            
            /*
                 pogram, subprogram, department, position and grade are in 
                 $request an get one by one*/
            $job->setProgram($req['program']);
            $job->setSubprogram($req['subprogram']);
            $job->setDepartment($req['department']);            
            $job->setPosition($req['position']);            
            $job->setGrade($req['grade']);
            $job->setCreatedBy($this->getEmpidAction());
            $job->setCreatedAt(new \DateTime());
            $em->persist($job);
            $em->flush();
            return $this->redirect($this->generateUrl('job'));                                     
        }

        return $this->render('TFSRiseBundle:Default:new.html.twig', array(
            'entity' => $job,
            'form'   => $form->createView(),
        ));                
    }

To me works.

1 Answer 1

0

It's maybe beacuse of the three entity does not have a __toString() method. If you use the entity form type, you have to set which entity property should be send to the database if it's not set, Symfony will use the __toString() method.

$builder->add('propgram', 'entity', array(
    'class' => 'TFSMaisBundle:DbasewuDepartment',
    'empty_value' => '',
    'property' => 'id'
));

If you set the 'property' option to 'id' for instance, than the select will send the entity id to the database.

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.