本文介绍了Symfony 集合表单 - 保存到 json_array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望创建字段集合并将其存储在一个 db 列类型 json_array 中?

Hove to create collections of field and store in one db column type json_array?

我的实体有一列 date_data,它是 json_array 类型.我想在前端渲染两个字段.

My entity have column date_data which is json_array type. I want to render two fields on frontent.

第一个字段 -> 来自 - 日期类型.
第二个字段 -> 到 - 日期类型.

First Field -> from - date type.
Second Field -> to - date type.

我使用 jQuery 中继器库,将此字段呈现为前端的中继器字段.并希望像这样将来自中继器的字段数据存储在 db 的 date_data 列中.

I use jQuery repeater lib, for render this fields as repeater field on frontend. And want to store fields data from repeater in date_data column in db like this.

[{"from": '12/31/2009' , "to": '01/16/2010' }, {"from": '02/10/2011', "to":'02/16/2011' }]

推荐答案

您可以使用 json 列为您的数据创建实体:

You can create entity with json column for your data:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Test
 *
 * @ORM\Table(name="test")
 * @ORM\Entity(repositoryClass="App\Repository\TestRepository")
 */
class Test
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", options={"unsigned":true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var array|null
     *
     * @ORM\Column(name="data", type="json", nullable=true)
     */
    private $data;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getData(): ?array
    {
        return $this->data;
    }

    public function setData(?array $data): self
    {
        $this->data = $data;

        return $this;
    }
}

和两种形式:第一个用于实体,第二个用于数据收集项:

and 2 forms: first for entity and second for data collection item:

应用\表单\测试

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type as FormType;

class Test extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('data', FormType\CollectionType::class, [
                'allow_add' => true,
                'allow_delete' => true,
                'entry_type' => 'App\\Form\\Data',
                'label' => 'Data',
            ])
            ->add('save', FormType\SubmitType::class, [
                'label' => 'Save',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'App\\Entity\\Test',
        ]);
    }
}

应用\表单\数据

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type as FormType;

class Data extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('from', FormType\TextType::class, [
                'label' => 'from',
            ])
            ->add('to', FormType\TextType::class, [
                'label' => 'to',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
    }
}

在控制器中

    $test = $this->getDoctrine()->getRepository('App:Test')->find(1);

    $form = $this->createForm(\App\Form\Test::class, $test, []);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        dump($form->getData());
        $this->getDoctrine()->getManager()->flush();
    }

这篇关于Symfony 集合表单 - 保存到 json_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:19