2015-11-27 46 views
0

我有一個表格由formbuilder表單提交錯誤data_class FormView控件

public function buildForm(FormBuilderInterface $builder, array $options){ 
    $query = $this->em->createQueryBuilder(); 
    $query->select('sn.serial_nr') 
      ->from('KopictAdminBundle:SerialNumber', 'sn'); 

    $serialnumbers = $query->getQuery()->getResult(); 

    $options = array(); 
    foreach($serialnumbers as $serialnumber){ 
     $options[$serialnumber['serial_nr']] = $serialnumber['serial_nr']; 
    } 

    $builder->add("serial_nr","text"); 
} 

它正確地顯示了形式建造,但是當我提交我得到這個錯誤:

"The form's view data is expected to be an instance of class Kopict\AdminBundle\Entity\SerialNumber, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Kopict\AdminBundle\Entity\SerialNumber." at /var/www/kopadmin/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 373 

這是怎麼了我的實體外觀像:

class SerialNumber 
{ 
    /** 
    * @var integer $id 
    */ 
    private $id; 

    /** 
    * @var interger $product_revision_id 
    */ 
    private $product_revision_id; 

    /** 
    * @var interger $booking_id 
    */ 
    private $booking_id; 

    /** 
    * @var string $serial_nr 
    */ 
    public $serial_nr; 


    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set product_revision_id 
    * 
    * @param integer $product_revision_id 
    * @return SerialNumber 
    */ 
    public function setProduct_revision_id($product_revision_id) 
    { 
     $this->product_revision_id = $product_revision_id; 

     return $this; 
    } 

    /** 
    * Get product_revision_id 
    * 
    * @return integer 
    */ 
    public function getProduct_revision_id() 
    { 
     return $this->product_revision_id; 
    } 

    /** 
    * Set booking_id 
    * 
    * @param integer $booking_id 
    * @return SerialNumber 
    */ 
    public function setBooking_id($booking_id) 
    { 
     $this->booking_id = $booking_id; 

     return $this; 
    } 

    /** 
    * Get booking_id 
    * 
    * @return integer 
    */ 
    public function getBooking_id() 
    { 
     return $this->booking_id; 
    } 

    /** 
    * Set serial_nr 
    * 
    * @param string $serial_nr 
    * @return SerialNumber 
    */ 
    public function setSerial_nr($serial_nr) 
    { 
     $this->serial_nr = $serial_nr; 

     return $this; 
    } 

    /** 
    * Get serial_nr 
    * 
    * @return string 
    */ 
    public function getSerial_nr() 
    { 
     return $this->serial_nr; 
    } 
} 

我試圖添加data_class,但我找不到添加它的好地方,因爲c頌歌不斷給我同樣的錯誤。

回答

0

首先,你需要讓你的serial_nrprivate否則不需要有getSerial_nrsetSerial_nr的功能。因爲你可以在你的班級以外達到serial_nr而不需要安裝者和獲得者。

二,您爲什麼要將serial numbers添加到選項字段中?

假設您想將serial numbers作爲選擇字段。我有你的解決方案。

通常在Doctrine ORM有關聯的實際物業爲many-to-oneone-to-many。在這種情況下,將相關領域作爲選擇領域非常簡單。對於這種情況,symfony有一個內置的entity字段。

SerialNumberType - 表單類的類。 (您必須將此名稱更改爲你的)

<?php 

namespace Kopict\AdminBundle\Form; 

use Doctrine\ORM\em; 
use Doctrine\ORM\EntityManager; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 

class SerialNumberType extends AbstractType 
{ 

    private $em; 

    public function __construct(EntityManager $em) 
    { 
     $this->em = $em; 
    } 


    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $query = $this->em->createQueryBuilder(); 
     $query->select('sn.serial_nr')->from('KopictAdminBundle:SerialNumber', 'sn'); 

     $serialNumbers = $query->getQuery()->getResult(); 
     $choices  = array(); 
     foreach ($serialNumbers as $serialNumber) { 
      $choices[$serialNumber['serial_nr']] = $serialNumber['serial_nr']; 
     } 
     $builder->add("serial_nr", "choice", array(
      'choices' => $choices, 
     )); 
    } 

    public function getName() 
    { 
     return 'app_bundle_serial_number_type'; 
    } 
} 

內部控制器動作

<?php 

namespace Kopict\AdminBundle\Controller; 

use Kopict\AdminBundle\Entity\SerialNumber; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class DefaultController extends Controller 
{ 
    public function indexAction() 
    { 
     $serialNumber = new SerialNumber(); 
     $form = $this->createForm($this->get('kopict_admin.form.serialnumber'), $serialNumber); 

     return $this->render('KopictAdminBundle:Default:index.html.twig', array('form' => $form->createView())); 
    } 
} 

services.yml

services: 
    kopict_admin.form.serialnumber: 
     class: Kopict\AdminBundle\Form\SerialNumberType 
     arguments: [ @doctrine.orm.entity_manager ] 
     scope: request