2015-11-16 27 views
1

我有一個表單上傳文件,我試圖「附加」正確的工作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函數嗎?

+0

我們可以看到完整的工作單位嗎? –

+0

你走了,這是相當大的:http://pastebin.com/hEFykA41 – PrimuS

+0

爲了您的更新:像每個行上的上傳文件按鈕的工作表? –

回答

1

嘗試在你的形式明確地定義你的領域job

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

PERFECT!因爲它已被棄用,所以將'property'改爲'choice_label',但它起作用。附加問題:如何填充多個表單?我會更新我的問題! – PrimuS

+0

重要提示:)我評論了您的更新 –

0

問題是find()返回數組,而不是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 
+0

雖然可能是正確的(而findOneBy我認爲),這不是什麼導致致命的,因爲它不會越過'$ form-> handleRequest($ request);' – PrimuS

0

你有插件( '工作', '文字')而絕有entity類型,而不是文字

先喲你需要在數據庫中有'工作'。然後你可以改變

$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(); 

您將收到選擇框在工作場