2016-05-13 167 views
-1

我試圖讓我的表單工作兩天以上。 我正在閱讀Symfony書中的「Forms」一章,但我不知道自己做錯了什麼。 這是我的實體類:使用Symfony和Doctrine創建表格

/** 
* Homecontent 
* 
* @ORM\Table(name="homecontent", indexes={@ORM\Index(name="id_idx", columns={"author_id"})}) 
* @ORM\Entity() 
*/ 
class Homecontent 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="home_id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $homeId; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date", type="datetime", nullable=false) 
    */ 
    private $date; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="text", nullable=false) 
    * 
    */ 
    private $title; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="content", type="text", nullable=false) 
    */ 
    private $content; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="picture_path", type="text", nullable=false) 
    */ 
    private $picturePath; 

    /** 
    * @var \Contentuser 
    * 
    * @ORM\ManyToOne(targetEntity="Contentuser") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="author_id", referencedColumnName="content_user_id") 
    * }) 
    */ 
    private $author; 

這是我的控制器:

/** 
    * @param $request 
    * @return \Symfony\Component\HttpFoundation\Response 
    * @Route("/content/insert") 
    */ 
    public function indexAction(Request $request) 
    { 

     $content = new HomecontentType(); 
     $form = $this->createForm(HomecontentType::class); 

     $form->handleRequest($request); 
     if ($form->isSubmitted() && $form->isValid()) { 
      $em = $this->getDoctrine() 
       ->getManager(); 
      $em->persist($content); 
      $em->flush(); 

      return $this->redirectToRoute('worked check index'); 
     } 
     return $this->render('Form/ContentForm.html.twig', array('form' => $form->createView())); 

這是我的Form類:

/** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title') 
      ->add('content') 
      ->add('picturePath') 
      ->add('date',DateTimeType::class) 
     ; 
    } 

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

我用樹枝進行渲染,它的工作,但是當我想提交它時,我收到一個錯誤

The class 'AppBundle\Form\HomecontentType' was not found in the chain configured namespaces AppBundle\Entity

對於長碼部分我很抱歉。 在此先感謝!

回答

1

在你有錯誤的indexAction的開始,正確的代碼是:

$content = new Homecontent(); 
$form = $this->createForm(HomecontentType::class, $content); 
+0

感謝您的快速回答,現在我得到一個新的錯誤: 表單的視圖數據預計將類AppBundle \ Entity \ Homecontent的一個實例,但是AppBundle \ Form \ HomecontentType類的一個實例。您可以通過將「data_class」選項設置爲null或通過添加將AppBundle \ Form \ HomecontentType類的實例轉換爲AppBundle \ Entity \ Homecontent實例的視圖轉換器來避免此錯誤。 – Kira

+1

你是否改變了兩條線?,你在前兩行有兩個錯誤 – Miro

+0

現在它的工作,我清除了Symfony Cache ...... 非常感謝! 對於我來說,你是否有任何tipps,如何處理表單中的關係以便與作者姓名一起下拉? – Kira