Php Symfony中需要不同的类型
我试图用符号和教义创造一种形式。 我使用Doctrine在mysql中创建了一个作业类和一个与其相关的表。它还创建了JobType和JobController以及路由工具 我可以访问列出作业的索引页面,但无法访问新条目页面 以下是用于创建表单的文件 JobController.phpPhp Symfony中需要不同的类型,php,mysql,symfony,doctrine-orm,Php,Mysql,Symfony,Doctrine Orm,我试图用符号和教义创造一种形式。 我使用Doctrine在mysql中创建了一个作业类和一个与其相关的表。它还创建了JobType和JobController以及路由工具 我可以访问列出作业的索引页面,但无法访问新条目页面 以下是用于创建表单的文件 JobController.php <?php namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Bun
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Job;
use AppBundle\Form\JobType;
/**
* Job controller.
*
* @Route("/job")
*/
class JobController extends Controller
{
/**
* Lists all Job entities.
*
* @Route("/", name="job_index")
* @Method("GET")
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$jobs = $em->getRepository('AppBundle:Job')->findAll();
return $this->render('job/index.html.twig', array(
'jobs' => $jobs,
));
}
/**
* Creates a new Job entity.
*
* @Route("/new", name="job_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$job = new Job();
$jobType = new JobType();
$form = $this->createForm($jobType, $job);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($job);
$em->flush();
return $this->redirectToRoute('job_show', array('id' => $job->getId()));
}
return $this->render('job/new.html.twig', array(
'job' => $job,
'form' => $form->createView(),
));
}
/**
* Finds and displays a Job entity.
*
* @Route("/{id}", name="job_show")
* @Method("GET")
*/
public function showAction(Job $job)
{
$deleteForm = $this->createDeleteForm($job);
return $this->render('job/show.html.twig', array(
'job' => $job,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing Job entity.
*
* @Route("/{id}/edit", name="job_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Job $job)
{
$deleteForm = $this->createDeleteForm($job);
$editForm = $this->createForm(new JobType(), $job);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($job);
$em->flush();
return $this->redirectToRoute('job_edit', array('id' => $job->getId()));
}
return $this->render('job/edit.html.twig', array(
'job' => $job,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Job entity.
*
* @Route("/{id}", name="job_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, Job $job)
{
$form = $this->createDeleteForm($job);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($job);
$em->flush();
}
return $this->redirectToRoute('job_index');
}
/**
* Creates a form to delete a Job entity.
*
* @param Job $job The Job entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Job $job)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('job_delete', array('id' => $job->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
谢谢
编辑:
app/config/services.yml的内容
parameters:
# parameter_name: value
services:
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
这在Symfony 3中不再可能。在Symfony 3中,始终必须传递表单类型的完全限定类名:
$editForm = $this->createForm(JobType::class, $job);
另外,在表单类型中,传递的是类型名称,而不是类型类的FQCN
Symfony 3刚刚发布了它的第一个测试版,这意味着它是非常前沿的。此外,Symfony 3几乎没有教程(因为它是非常前沿的)。您正在阅读Symfony 2教程,因此我建议您安装Symfony 2而不是3。您能显示作业类型的服务定义吗这是我在app/config/services.yml
中找到的内容#了解有关服务的更多信息,#处的参数和容器http://symfony.com/doc/current/book/service_container.html 参数:#参数名称:值服务:#服务名称:#类:AppBundle\Directory\ClassName#参数:[“@other_service_name”、“plain_value”、“参数名称%”]我应该看看其他地方吗?考虑更新问题本身(在它下面有一个编辑按钮)和服务定义。正如您所看到的,注释的格式不好。从代码中可以清楚地看出,您没有一个JobType服务,这很好。我看不出你的代码有任何明显的错误。您可以尝试注释除一条之外的所有add语句以简化操作。我只留下了一条add语句,并尝试替换“string”、“text”和一条空语句,结果返回了相同的错误。从错误判断,似乎应该发送一个字符串来代替newjobType:$form=$this->createForm(newjobType(),$job)你认为这和那有什么关系吗?是的。只是做了一些调查。看起来3.0中有很多表单组件更改。[已解决]是的,这就是问题所在。谢谢大家!:)为了安全起见,我将再次从Symfony 2开始。@TheodorMoraru如果这个答案解决了您的问题,请将其标记为已解决。
$editForm = $this->createForm(new JobType(), $job);
$editForm = $this->createForm(JobType::class, $job);