Search code examples
symfonysonata-adminsymfony-sonata

Sonata admin display string matching integer values


I'm using sonata admin for my backoffice application. My entity has an integer field, each value has a meaning that I'd like to display in words. For instance, say that it has the opinion field, which can be 0, 1, 2 or 3. 0 means no opinion 1 means I agree 2 means I don't agree 3 means I've got to explain

In my db I store 0, 1, 2 or 3 and on the CRUD interfaces I need to display the string values

How do I do that?


Solution

  • There are several ways to do it , this is one approach

    In your entity define constants like so :

    Class EntityName
    {
        const NO_OPINION = 0;
        const I_AGREE = 1;
        const I_DONT_AGREE = 2;
        const I_GOTTA_EXPLAIN = 3;
    
       // field which holds those values
       protected $status = EntityName::NO_OPINION;
    
    
    //
    

    In the admin class

    $listMapper
        ->addIdentifier('id')
        ->add('status', 'choice', array(
        'choices' => array(
        EntityName::NO_OPINION => "No Opinion",
        EntityName::I_AGREE => 'I Agree',
        EntityName::I_DONT_AGREE => 'I Don\'t Agree',
        EntityName::I_GOTTA_EXPLAIN => 'I\'ve got to explain', 
        )
        ))
        ->add('somethingelse')