Home > Blockchain >  Show specific fields in related entities in choicetype Symfony 5
Show specific fields in related entities in choicetype Symfony 5

Time:09-24

I am generating a form that its field are related to many entities, for example, I can only show some fields in entity NatRec if the related column of Entity Gerance is the same one in the NatRec Entity. The code below can show you how things works:

Entity Gerance.php

<?php
// Entity Gerance
namespace App\Entity;

use App\Repository\GeranceRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=GeranceRepository::class)
 */
class Gerance
{
    /**
     * @ORM\Id
     * @ORM\Column(name="CODEGERA")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=30)
     */
    private $LIBEGERA;

    /**
     * @ORM\Column(type="string", length=1)
     */
    private $NUMEGERA;///// related to NatRec

    public function getId(): ?string
    {
        return $this->id;
    }

    public function getLIBEGERA(): ?string
    {
        return $this->LIBEGERA;
    }

    public function setLIBEGERA(string $LIBEGERA): self
    {
        $this->LIBEGERA = $LIBEGERA;

        return $this;
    }

    public function getNUMEGERA(): ?string
    {
        return $this->NUMEGERA;
    }

    public function setNUMEGERA(?string $NUMEGERA): self
    {
        $this->NUMEGERA = $NUMEGERA;

        return $this;
    }

}

Entity NatRec.php

<?php

namespace App\Entity;

use App\Repository\NatRecRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=NatRecRepository::class)
 */
class NatRec
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer", name="CODNATRE")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=150, nullable=true)
     */
    private $LIBNATRE;

    /**
     * @ORM\Column(type="string", length=1)
     */
    private $NUMEGERA;

    /**
     * @ORM\Column(type="string", length=200)
     */
    private $LIBELLE;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLIBNATRE(): ?string
    {
        return $this->LIBNATRE;
    }

    public function setLIBNATRE(?string $LIBNATRE): self
    {
        $this->LIBNATRE = $LIBNATRE;

        return $this;
    }

    public function getNUMEGERA(): ?string
    {
        return $this->NUMEGERA;
    }

    public function setNUMEGERA(?string $NUMEGERA): self
    {
        $this->NUMEGERA = $NUMEGERA;

        return $this;
    }

    public function getLIBELLE(): ?string
    {
        return $this->LIBELLE;
    }

    public function setLIBELLE(string $LIBELLE): self
    {
        $this->LIBELLE = $LIBELLE;

        return $this;
    }
}

And the selected filed has to be related. Image of the select fields

Image of the select fields

For the sake of a problem that was generated from the database I couldn't make the entities relate in doctrine, so I can only compare fields of each.

ReclamationType.php:

<?php

namespace App\Form;

use App\Entity\Gerance;
use App\Entity\NatRec;
use App\Entity\Reclamation;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ReclamationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('adressere')
            ->add('numetelep')
            ->add('numerofax')
            ->add('adresmail')
            ->add('objreclam')
            ->add('numegera', EntityType::class, [
                'class' => Gerance::class,
                'choice_label' => 'LIBEGERA'
            ])
            ->add('codnatre', EntityType::class,[
                'class' => NatRec::class,
                'choice_label' => 'LIBNATRE'
            ])
            ->add('prenom')
            ->add('cin')
            ->add('datesais')
            ->add('codeclie')
            ->add('usersais')
            ->add('codesect')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Reclamation::class,
        ]);
    }
}

Hopefully someone can help me through this.

CodePudding user response:

So I happen to have found a solution by getting all the data I want to display from the repositories of the entities I needed and making conditions in JavaScript by getting elements ids and changing there values, it was just a bit harder with Symfony's forms since I didn't know how to get the id of the selected options until I inspected the elements. I'm glad I managed to answer this on my own.

  • Related