Symfony收集表单-保存到json_array

时间:2019-05-13 19:44:58

标签: symfony symfony-forms

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

我的实体有date_data列,它是json_array类型。我想在frontent上渲染两个字段。

第一个字段->来自-日期类型。
第二字段->至-日期类型。

我使用jQuery转发器库,将此字段呈现为前端的转发器字段。并希望将中继器的字段数据存储在db的date_data列中。

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

1 个答案:

答案 0 :(得分:1)

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

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 string
     *
     * @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;
    }
}

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

App \ Form \ 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 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',
        ]);
    }
}

App \ Form \ Data

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