我有一個表單上傳文件,我試圖「附加」正確的工作ID /實體,但它似乎我不完全理解這個概念表關係:發送OneToMany到數據庫失敗的表格(Symfony2/Doctrine)
我的文件類
/**
* @ORM\ManyToOne(targetEntity="Job", inversedBy="file")
*/
protected $job;
我的作業類:
/**
* @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();
}
發送,但它甚至可以給點?
我在做什麼錯?
任何暗示讚賞!
UPDATE
感謝@朱利安 - 布爾迪克我更新了我的形式是這樣的:
/**
* @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(),
)
);
}
這個類的全部目的是爲了有一個表,其中最後一個按鈕每一行都是一個上傳表格。我如何從一個類中填充多個表單,這甚至可能嗎?我必須發送到render
函數嗎?
我們可以看到完整的工作單位嗎? –
你走了,這是相當大的:http://pastebin.com/hEFykA41 – PrimuS
爲了您的更新:像每個行上的上傳文件按鈕的工作表? –