如何在symfony 2.4中插入数据形式选择字段

时间:2014-06-24 23:03:34

标签: php mysql forms doctrine symfony-2.4

我的symfony2.4项目中有下一个表单类型,我使用的是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';
    }
}

这是控制器:

<?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(),
        ));

    }        

}

我的问题是当我尝试插入选择字段的值时:程序,部门和位置,因为表单将值0发送到数据库,而另一个字段我没有遇到问题。

表单中的选择字段由一个包(MaisBundle)填充,必须插入另一个包(RiseBundle)

我感谢任何帮助。

感谢。

我找到了一个解决方案,现在控制器中的newAction(Request $ request)就像:

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

对我而言。

1 个答案:

答案 0 :(得分:0)

可能因为这三个实体没有__toString()方法。 如果您使用实体表单类型,则必须设置应将哪个实体属性发送到数据库(如果未设置),Symfony将使用__toString()方法。

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

例如,如果将'property'选项设置为'id',则select会将实体ID发送到数据库。