Symfony集合表单 - 保存为json_array

3
如何创建字段集合并将其存储在一个数据库列类型为json_array中?
我的实体具有json_array类型的date_data列。我想在前端呈现两个字段。
第一个字段->来自-日期类型。 第二个字段->到-日期类型。
我使用jQuery repeater库,在前端呈现这些字段作为repeater字段。并希望像这样将来自repeater的字段数据存储在db中的date_data列中。
[{"from":'12/31/2009',"to":'01/16/2010'},{"from":'02/10/2011',"to":'02/16/2011'}]
1个回答

5
您可以使用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 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;
    }
}

有两种表单:第一种是用于实体,第二种是用于数据收集项:

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

应用程序\Form\数据

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

1
谢谢。我觉得它可以工作。但是我不知道如何在.twix中渲染表单。我使用了以下代码:{{ form_start(form) }} {{ form_row(form) }}
    {% for fields in form.data %}
  • {{ form_errors(fields) }} {{ form_widget(fields) }}
  • {% endfor %}
{{ form_end(form) }}
但是在浏览器中看不到字段。
- undefined
我知道,但在DOM中我看到了<form>标签和其他东西。但是我无法在集合中找到字段。from和to字段。 - undefined
不工作。渲染带有填充了一些HTML代码的data-prototype属性的标签和div。同时渲染提交按钮,但不包括集合中的字段。 - undefined
1
似乎数据字段为空。尝试设置一些测试值。例如:[{"to": "01/16/2010", "from": "12/31/2009"}, {"to": "02/16/2011", "from": "02/10/2011"}]。还要清除Doctrine元数据缓存 php ./bin/console doctrine:cache:clear-metadata - undefined
好的!非常感谢! - undefined
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接