在SonataAdmin列表视图中显示名称而不是DB值

时间:2014-02-18 00:13:31

标签: symfony sonata-admin symfony-sonata

当我尝试列出来自DB的记录时,我遇到了问题,因为我得到的行存储在行中,这是一个整数,但我想要字符串。我是如何处理表格的?这样:

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(在视图中相当于BC)定义为选择,如何显示字符串而不是列表视图中的值?请参阅附图,了解问题的可视化

enter image description here

1 个答案:

答案 0 :(得分: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];
    }

}

在您的Admin类

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));
       ....
   }
}