Using Sonata Admin to work with Doctrine json_array fields

我怕爱的太早我们不能终老 提交于 2019-12-31 04:15:18

问题


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 the Symfony / Sonata docs (or code), but I'm hoping that I missed something!

Ideally I would like to see the serialised key/value pairs with a way to delete any of them and add some new ones, but I guess I'm going to have to write that up myself...


回答1:


You work with json_array just like with any array so You can go with collection type or sonata type collection. I like that sonata type takes care about add/delete JavaScript for You.




回答2:


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


来源:https://stackoverflow.com/questions/21817255/using-sonata-admin-to-work-with-doctrine-json-array-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!