Вы можете создать сущность со столбцом 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',
]);
}
}
Приложение \ Форма \ Данные
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();
}