gpt4 book ai didi

php - 如何从 symfony 2.4 中的选择字段插入数据

转载 作者:行者123 更新时间:2023-11-30 00:07:30 24 4
gpt4 key购买 nike

我的 symfony2.4 项目中有下一个表单类型,我使用 ORM Doctrine

<?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';
}
}

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

}

}

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

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

感谢任何帮助。

谢谢。

我找到了一个解决方案,现在 Controller 中的 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(),
));
}

对我来说有效。

最佳答案

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

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

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

关于php - 如何从 symfony 2.4 中的选择字段插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com