3

Hove to create collections of field and store in one db column type 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.

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' }]

1 Answer 1

5

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:

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)
    {
    }
}

And in controller

    $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();
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Tnx. I think it works. But I do not know how to render form in .twix. I use {{ form_start(form) }} {{ form_row(form) }} <ul> {% for fields in form.data %} <li> {{ form_errors(fields) }} {{ form_widget(fields) }} </li> {% endfor %} </ul> {{ form_end(form) }} and I don't see fields in browser.
you can render whole form {{ form(form) }}
I know, but in dom i se <form> tag and other things. But i can't se fields in folection. from and to fields
try <li> {{ form_errors(fields) }} {{ form_errors(fields.from) }} {{ form_widget(fields.from) }} {{ form_errors(fields.to) }} {{ form_widget(fields.to) }} </li>
Seems data field is emty. Try to set some test value. Ex.: [{"to": "01/16/2010", "from": "12/31/2009"}, {"to": "02/16/2011", "from": "02/10/2011"}]. Also clean doctrine metadata cache php ./bin/console doctrine:cache:clear-metadata
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.