symfonysonata-adminsymfony-sonata

Display name instead of DB value in SonataAdmin List View


I'm having a problem when I try to list the record from DB since I get the value stored in row which is a integer but I want the string instead. How I'm handling the form? In this way:

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

I define charge_status (is the equivalent to BC in view) as choice, how I can show the string and not the value in the list view? See the attached image for visual of the problem

enter image description here


Solution

  • One approach:

    define a static array in your entity for example:

    <?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];
        }
    
    }
    

    In your Admin class

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