发送 OneToMany 到数据库的表单失败(Symfony2 / Doctrine)

Form sending OneToMany to Database fails (Symfony2 / Doctrine)

我有一个上传文件的表格,我正在尝试 "attach" 正确的工作 id/entity 给它,但我似乎没有完全理解 table关系:

我的文件class

/**
 * @ORM\ManyToOne(targetEntity="Job", inversedBy="file")
 */
protected $job;

我的工作class:

/**
 * @ORM\OneToMany(targetEntity="File", mappedBy="job")
 */
protected $file;

public function __construct()
{
    $this->file = new ArrayCollection();
}

我正在提交表格并将所有内容输入数据库:

$em = $this->getDoctrine()->getManager();
    $file = new File();
    $form = $this->createFormBuilder($file)
        ->add('file')
        ->add('job','text')
        ->add('save', 'submit', array('label' => 'Create Task'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {


        $job = $em->getRepository("AppBundle:Job")->find($form->getData()->getJob());

        $file->setFile($form->getData()->getFile());
        $file->setPath($form->getData()->getPath());
        $file->setJob($job);

        $em->persist($file);
        $em->flush();

        return $this->redirectToRoute("pendingJobs");
    }

提交表单以致命错误结束:

Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\File::setJob() must be an instance of AppBundle\Entity\Job, string given, called in /var/www/html/web2gdv/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined

我尝试调试用

发送的内容
if ($form->isValid()) {

        dump($form->getData());
        die();
}

但它甚至到了重点?!

我做错了什么?

感谢任何提示!

更新

感谢@julien-bourdic,我更新了我的表格:

 /**
 * @Route("/job/pending", name="pendingJobs")
 */
public function jobAction(Request $request)
{
    $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');

    $em = $this->getDoctrine()->getManager();
    $file = new File();
    $form = $this->createFormBuilder($file)
        ->add('file')
        ->add('job','entity',array(
            'class' => 'AppBundle:Job',
            'choice_label' => 'id',
        ))
        ->add('save', 'submit', array('label' => 'Create Task'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {

        $job = $em->getRepository("AppBundle:Job")->find($form->getData()->getJob());

        $file->setFile($form->getData()->getFile());
        $file->setPath($form->getData()->getPath());
        $file->setJob($job);

        $em->persist($file);
        $em->flush();

        return $this->redirectToRoute("pendingJobs");
    }



    $jobs = $em->getRepository("AppBundle:Job")->findBy(array(
        'receipt' => true,
        'receiptStatus' => true,
    ));

    return $this->render(
        'default/pending.html.twig',
        array(
            'jobs' => $jobs,
            'form' => $form->createView(),
        )
    );


}

这个class的全部目的是有一个table,其中每行的最后一个按钮是一个上传表单。我如何从一个 class 填充多个表单,这可能吗?我必须将什么发送到 render 函数?

问题是 find() returns 数组,而不是 Job。使用 findOne().

$job = $em->getRepository("AppBundle:Job")->find($form->getData()->getJob());
// $job is and Array

改为

$job = $em->getRepository("AppBundle:Job")->findOne($form->getData()->getJob());
// $job is Job

尝试在表单中明确定义字段 job

->add('job','entity',array(
        'class'=>'AppBundle:Job',
        'property'=>'id',
)

您有 add('job', 'text') 并且必须有 entity 类型,而不是文本

首先你需要在数据库中有一个'jobs'。那你可以改成

$form = $this->createFormBuilder($file)
    ->add('file')
    ->add('job','entity', array('class' => 'YourBundle:Job'))
    ->add('save', 'submit', array('label' => 'Create Task'))
    ->getForm();

或短

$form = $this->createFormBuilder($file)
    ->add('file')
    ->add('job')
    ->add('save', 'submit', array('label' => 'Create Task'))
     ->getForm();

您将在工作字段上收到 select 框