所以我现在已经尝试了一段时间来修改symfonys基本crud系统以满足我的需求。但是我无法理解为什么在获取形式中他们定义实体然后在帖子中他们再次写实体?
我有一个关系,将1个工作与多个工作项目联系起来,我想要修改的是创建'工作'这是从作业项创建的,jobId是通过dash_work_new路径传递的。
选项1
我管理的是从传递的变量加载我的作业实体并设置我的工作项,如$ entity-> setWorkJob()。大?不,因为在提交时调用了dash_work_create路由,为什么?工作实体又被定义了吗?因此擦除我在newAction()方法中设置的任何内容
选项2
所以我认为我会将变量作为隐藏字段传递,然后在submit上加载所有数据,但是createCreateForm上的挂起也会在createAction中重新定义,所以如果我尝试通过createCreateForm传递变量($新行动中的实体,$ jobId)。我还需要在createAction中定义它,但我没有.... AH%#@@ EXPLETIVE EXPLETIVE EXPLETIVE
AH沮丧和缺乏理解。供参考symfony workController由console crud生成。我离开了从@route new到@route new / {jobId}
的改变的新路线非常感谢您的帮助
/**
* Creates a new Work entity.
*
* @Route("/", name="dash_work_create")
* @Method("POST")
* @Template("LaelapsInvoiceBundle:Work:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Work();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('dash_job'));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Work entity.
*
* @param Work $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Work $entity)
{
$form = $this->createForm(new WorkType(), $entity, array(
'action' => $this->generateUrl('dash_work_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Work entity.
*
* @Route("/new/{jobId}", name="dash_work_new")
* @Method("GET")
* @Template()
*/
public function newAction($jobId)
{
$entity = new Work();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
答案 0 :(得分:1)
在您的POST操作中,创建表单框架:
$entity = new Work();
$form = $this->createCreateForm($entity);
因为处理请求需要它,所以后面的表单填充了请求数据:
$form->handleRequest($request);
现在,您的表单中包含您在newAction()
中提供的数据,之后您将对其进行验证:
$form->isValid()
现在,足够的理论。我看到你只想将jobId
传递给你的POST动作。最简单的方法是通过url,你所要做的就是改变表单的路由和动作。 POST操作可能如下所示:
/**
* Creates a new Work entity.
*
* @Route("/{jobId}", name="dash_work_create")
* @Method("POST")
* @Template("LaelapsInvoiceBundle:Work:new.html.twig")
*/
public function createAction(Request $request, $jobId)
{
$entity = new Work();
$form = $this->createForm(new WorkType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
//fetch you Job entity by $jobId and do what you need.
$job = $em->getRepository('MyBundle\Entity\Job')->findOneById($jobId);
$entity->setWorkJob($job);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('dash_job'));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
新动作:
/**
* Displays a form to create a new Work entity.
*
* @Route("/new/{jobId}", name="dash_work_new")
* @Method("GET")
* @Template()
*/
public function newAction($jobId)
{
$entity = new Work();
$form = $this->createForm(new WorkType(), $entity,
array(
'action' => $this->generateUrl('dash_work_create', array('jobId' => $jobId ))
));
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}