Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

transforming a set of form inputs into JSON

$
0
0

So this is going to sound a bit complicated since I'm a total noob to JSON but bear with me guys, I really need this.let me start by showing my entity

<?phpnamespace App\Entity;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass="App\Repository\TypeParkingRepository") */class TypeParking{    /**     * @ORM\Id()     * @ORM\GeneratedValue()     * @ORM\Column(type="integer")     */    private $id;    /**     * @ORM\Column(type="string", length=55)     */    private $libelle;    /**     * @ORM\Column(type="date", nullable=true)     */    private $jourdebut;    /**     * @ORM\Column(type="date", nullable=true)     */    private $jourfin;    /**     * @ORM\Column(type="json_array", nullable=true)     */    private $exception;    public function getId(): ?int    {        return $this->id;    }    public function getJourdebut(): ?\DateTimeInterface    {        return $this->jourdebut;    }    public function setJourdebut(\DateTimeInterface $jourdebut): self    {        $this->jourdebut = $jourdebut;        return $this;    }    public function getJourfin(): ?\DateTimeInterface    {        return $this->jourfin;    }    public function setJourfin(\DateTimeInterface $jourfin): self    {        $this->jourfin = $jourfin;        return $this;    }    public function getException() {        return array_merge(['name' => null,'datedebut' => null, 'datefin' => null, 'heuredebut' => null, 'heurefin' => null,             // other sub-fields "empty" values        ], $this->exception ?? [] // prevent array_merge from failing if exception is empty        );    }    public function setException($exception): self    {        $this->exception = $exception;        return $this;    }    public function getLibelle(): ?string    {        return $this->libelle;    }    public function setLibelle(string $libelle): self    {        $this->libelle = $libelle;        return $this;    }}

As you can see I have this property called exception, now this property has many sub properties such as name, datedebut,datefin...So I had to use JSON format for it.so I created this formtype called TypeParkingType.php

<?phpnamespace App\Form;use Symfony\Component\Form\Extension\Core\Type\TextType;use Symfony\Component\Form\Extension\Core\Type\DateTimeType;use Symfony\Component\Form\Extension\Core\Type\TimeType;use Symfony\Component\Form\Extension\Core\Type\DateType;use App\Entity\TypeParking;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolver;class TypeParkingType extends AbstractType{    public function buildForm(FormBuilderInterface $builder, array $options)    {        $builder            ->add('libelle')            ->add('tempsmax')            ->add('jourdebut')            ->add('jourfin')            ->add('jourstravail_jour', TextType::class, ['property_path' => 'jourstravail[jour]'])            ->add('jourstravail_debut', TextType::class, ['property_path' => 'jourstravail[debut]'])            ->add('jourstravail_fin', TextType::class, ['property_path' => 'jourstravail[fin]'])            ->add('Exception_Name', TextType::class, ['property_path' => 'exception[name]'])            ->add('Starting_date', DateTimeType::class, ['property_path' => 'exception[datedebut]',            ])            ->add('Ending_date', DateTimeType::class, ['property_path' => 'exception[datefin]',            ])            ->add('Starting_time', TextType::class, ['property_path' => 'exception[heuredebut]'])            ->add('Ending_time', TextType::class, ['property_path' => 'exception[heurefin]'])        ;    }    public function configureOptions(OptionsResolver $resolver)    {        $resolver->setDefaults(['data_class' => TypeParking::class,        ]);    }}

Sadly this doesn't work because even after inserting the data into the database it does so in a impractical way that's impossible to read.To make it more complicated, each form can have more than one exception meaning that I have to parse the data in a way like: Ex: {exceptionName(datedebut:...,datefin...,time:...),exceptionname2....}

what I mean by impossible is that it gives me a string like this one https://i.sstatic.net/emsYY.jpg which:

  1. gives me this error when I go to the edit page for the chosen type: Unable to transform value for property path "exception[datedebut]": Expected a \DateTimeInterface.

  2. I can't extract the data one by one from it

TL;DR: how can I insert the values related to exception into a single JSON string in the database.


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>