在选择类型 Symfony 5 中显示相关实体中的特定字段
Show specific fields in related entities in choicetype Symfony 5
我正在生成一个表单,它的字段与许多实体相关,例如,如果实体 Gerance 的相关列与 NatRec 实体中的列相同,我只能显示实体 NatRec 中的某些字段。下面的代码可以向您展示它是如何工作的:
实体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;
}
}
实体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;
}
}
并且所选的文件必须是相关的。
Image of the select fields
Image of the select fields
由于数据库生成的问题,我无法使实体在学说上相关,所以我只能比较每个实体的字段。
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,
]);
}
}
希望有人能帮助我解决这个问题。
所以我碰巧找到了一个解决方案,方法是从我需要的实体的存储库中获取我想要显示的所有数据,并通过获取元素 ID 并更改那里的值在 JavaScript 中设置条件,它是使用 Symfony 的表单有点困难,因为在检查元素之前我不知道如何获取所选选项的 ID。很高兴我自己设法回答了这个问题。
我正在生成一个表单,它的字段与许多实体相关,例如,如果实体 Gerance 的相关列与 NatRec 实体中的列相同,我只能显示实体 NatRec 中的某些字段。下面的代码可以向您展示它是如何工作的:
实体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;
}
}
实体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;
}
}
并且所选的文件必须是相关的。 Image of the select fields
Image of the select fields
由于数据库生成的问题,我无法使实体在学说上相关,所以我只能比较每个实体的字段。
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,
]);
}
}
希望有人能帮助我解决这个问题。
所以我碰巧找到了一个解决方案,方法是从我需要的实体的存储库中获取我想要显示的所有数据,并通过获取元素 ID 并更改那里的值在 JavaScript 中设置条件,它是使用 Symfony 的表单有点困难,因为在检查元素之前我不知道如何获取所选选项的 ID。很高兴我自己设法回答了这个问题。