php - Symfony 中预期的不同类型

标签 php mysql symfony doctrine-orm

我正在尝试使用 Symfony 和 Doctrine 创建一个表单。 我使用 Doctrine 创建了一个 Job 类,并在 mysql 中创建了一个与其相关的表。它还创建了 JobType 和 JobController 以及路由设施。

我可以访问列出职位的索引页面,但无法访问新的条目页面。

以下是用于创建表单的文件。

JobController.php

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

JobType.php

namespace AppBundle\Form;

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

class JobType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('category', 'string')
            ->add('type', 'string')
            ->add('company', 'string')
            ->add('logo', 'string')
            ->add('url', 'string')
            ->add('position', 'string')
            ->add('location', 'string')
            ->add('desciption', 'text')
            ->add('how_to_apply', 'text')
            ->add('token', 'string')
            ->add('is_public', 'boolean')
            ->add('is_activated', 'boolean')
            ->add('email', 'string')
            ->add('expires_at', 'datetime')
            ->add('created_at', 'datetime')
            ->add('updated_at', 'datetime')
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(   
            'data_class' => 'AppBundle\Entity\Job'
        ));
    }   

    /**
     * Mandatory in Symfony2
     * Gets the unique name of this form.
     * @return string
     */
    public function getName()
    {
        return 'add_job';
    }
}

This is the error I receive 谢谢!

编辑: app/config/services.yml的内容

parameters:
#    parameter_name: value

services:
#    service_name:
#        class: AppBundle\Directory\ClassName
#        arguments: ["@another_service_name", "plain_value", "%parameter_name%"]

最佳答案

$editForm = $this->createForm(new JobType(), $job);

这在 Symfony 3 中不再可能。在 Symfony 3 中,您始终必须传递表单类型的完全限定类名:

$editForm = $this->createForm(JobType::class, $job);

此外,在表单类型中,您将传递类型名称而不是类型类的 FQCN。

Symfony 3 刚刚发布了它的第一个 BETA,这意味着它非常前沿。此外,Symfony 3 的教程几乎为零(因为它非常前沿)。您正在阅读 Symfony 2 教程,因此我建议您安装 Symfony 2 而不是 3。

关于php - Symfony 中预期的不同类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843888/

相关文章:

mysql - SQL 连接和排序

Mysql查询从表中获取客户数量

php - 服务中的 Symfony4 findAll 返回空数组

symfony - 使用 Monolog 在 Symfony2 中记录 PHP fatal error

javascript - 获取 div 内的子元素? javascript

PHP:foreach msqli_query 问题

php - 无法使用登录功能

php - 将 Start_date 与 SQL 中的当前日期进行比较

symfony - 使用多个时自动连接特定的 DBAL 连接

php - 使用迁移添加约束 - yii 框架