Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony render json_array entity type and save using form

Imagine I have an Article entity.
And in this entity have a report attribute which is an json_array type.

Json_array's data may like
{"key1":"value1","key2":{"k1":"v1","k2","v2"...},"key3":["v1","v2","v3"...]...}.
I mean the json_array may contains simple key:value
or the value may also contains key:vaule
or the value may an array.

Now I don't know how to use symfony form to render and save these json_array like other normal attribute(e.g.,title).
At the same time,I want to manage the key label name with an meaning name just like change the title field's label.
How to achieve this,I feel very difficult.

class Article
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var array
     *
     * @ORM\Column(name="report", type="json_array")
     */
    private $report;

}
like image 994
jmz Avatar asked Jun 01 '26 19:06

jmz


1 Answers

Maybe you can use json_decode to pass from json to array and later in the form you can use:

 ->add('someField', null, array('mapped' => false))

And in the success do something with this values

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// some awesome code here
}

Hope this can help you.

Roger

like image 148
Roger Guasch Avatar answered Jun 03 '26 12:06

Roger Guasch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!