2014-02-18 58 views
3

我在嘗試列出來自數據庫的記錄時出現問題,因爲我得到的值存儲在行中,但這是一個整數,但我想要字符串。我如何處理表單?通過這種方式:在SonataAdmin列表視圖中顯示名稱而不是數據庫值

protected function configureFormFields(FormMapper $form) { 
    $form 
      ->add('no_order', null, array('label' => 'No. Order')) 
      ->add('company', 'entity', array('class' => 'PL\CompanyBundle\Entity\Company', 'label' => 'Cliente')) 
      ->add('business_case', null, array('label' => 'BC')) 
      ->add('charge_status', 'choice', array('choices' => array(
        "empty_value" => "Seleccione una opción", 
        "0" => "Ninguno", 
        "1" => "Proceso de Fabricacion", 
        "2" => "Pickup en destino", 
        "3" => "A la espera de recojo por cliente", 
        "4" => "Carga en transito", 
        "5" => "Carga arribada", 
        "6" => "En proceso de aduana", 
        "7" => "Entregado a cliente", 
        "8" => "En bodega" 
       ), "required" => true, 'label' => 'Estado de la carga')) 
      ->add('eta', null, array('label' => 'ETA', 'widget' => 'single_text', 'required' => false, 'attr' => array('class' => 'datepicker'))) 
      ->add('etd', null, array('label' => 'ETD', 'widget' => 'single_text', 'required' => false, 'attr' => array('class' => 'datepicker'))) 
      ->add('transport_media', 'choice', array('choices' => array("empty_value" => "Seleccione una opción", "0" => "EXW", "1" => "Maritimo", "2" => "Aereo"), "required" => true, 'label' => 'Via de Transporte')) 
      ->add('incoterm', 'choice', array('choices' => array(
        "empty_value" => "Seleccione una opción", 
        "0" => "Ninguno", 
        "1" => "EWX", 
        "2" => "FOB", 
        "3" => "CIF", 
        "4" => "DDP" 
       ), "required" => true, 'label' => 'Incoterm')) 
      ->add('comments', null, array('label' => 'Comentarios')) 
      ->with('Documentos') 
      ->add('medias', 'sonata_type_collection', array(
       'required' => false), array(
       'edit' => 'inline', 
       'inline' => 'table', 
       'sortable' => 'position')) 
      ->end(); 
} 

我定義charge_status(是相當於公元前視圖)的選擇,我怎麼能顯示在列表視圖中的字符串,而不是價值?見所附的圖像進行視覺問題的

enter image description here

+0

'empty_value'不應該存在於選擇數組中,而應該是一個單獨的opti在主選項數組中。 – nifr

+0

@nifr謝謝我解決了這個問題 – Reynier

+0

這種行爲很奇怪......你嘗試在這些選擇數組中翻轉鍵和值嗎?你究竟從哪裏獲取數據庫?我無法在你的代碼中找到它。 – nifr

回答

2

一種方法:

在實體定義一個靜態數組,例如:

<?php 
class Entity 
{ 
    public static $chargeStatusList = array(
       "0" => "Ninguno", 
       "1" => "Proceso de Fabricacion", 
       "2" => "Pickup en destino", 
       "3" => "A la espera de recojo por cliente", 
       "4" => "Carga en transito", 
       "5" => "Carga arribada", 
       "6" => "En proceso de aduana", 
       "7" => "Entregado a cliente", 
       "8" => "En bodega" 
      ); 

    public function getChargeStatusValue() 
    { 
     return self::$chargeStatus[$this->charge_status]; 
    } 

} 

在管理員類

class YourAdmin extends Admin 
{ 

    .... 
    use Path/To/Your/Entity; 
    protected function configureListFields(ListMapper $listMapper) 
    { 
     $listMapper 
      ->add('chargeStatusValue', 'string', array('label' => 'Charge status')) 

    ; 
    } 

    protected function configureFormFields(FormMapper $form) 
    { 
     .... 
     ->add('charge_status', 'choice', array('choices' => Entity::$chargeStatusList)); 
     .... 
    } 
} 
+0

) – Reynier

相關問題