Using Sonata Admin to work with Doctrine json_array fields

前端 未结 2 1201
清酒与你
清酒与你 2021-01-07 04:41

Is there any \'built in\' way to administer Doctrine json_array fields using Sonata Admin?

I can\'t find any useful documentation (or relevant code) in

2条回答
  •  有刺的猬
    2021-01-07 05:01

    In my entity I work with "json" type on column "config"

    /**
     * @var integer
     *
     * @ORM\Column(name="config", type="json", nullable=false)
     */
    protected $config;
    

    You can use basic DataTransformer to transform your value from JSON to string and display it as text in form

    $formMapper->add('config', 'text', [
        'required' => false,
    ])        
    $formMapper->get('config')->addModelTransformer(new CallbackTransformer(
        function ($tagsAsArray) {
            //object stdclass json, need to be transform as string for render form
            return json_encode($tagsAsArray);
        },
        function ($tagsAsString) { 
            //string, need to be transform as stdClass for json type for persist in DB
            return json_decode($tagsAsString);
        }
    ));
    

提交回复
热议问题