Symfony Dynamic Form显示相关数据的问题

yfjy0ee7  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(113)

我有一个表格,我想显示与该地区有关的那些村庄(村庄),但它给了我以下错误。
最初我认为错误一定是由于addListener,但当我评论时,错误仍然存在。
应为“string”类型的参数,“App\Entity\District”在属性路径“district”处给出。
以下是代码:
形式

->add('district', EntityType::class, [
            'class' => District::class,
            'choice_label' => function (District $district){
                return $district->getDistrictName();
            }
        ])
    ;

    $formModifier = function (FormInterface $form, District  $district  = null): void {
        $positions = null === $district ? [] : $district->getVillages();

        $form->add('village', EntityType::class, [
            'class' => Village::class,
            'placeholder' => '',
            'choices' => $positions,
        ]);
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
            $form = $event->getForm();
            $data = $event->getData();
        
            $formModifier($event->getForm(), $data->getVillage());
        }
    );

    $builder->get('district')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifier) {
            $form = $event->getForm();
            $formModifier($event->getForm()->getParent(), $event->getForm()->getData());
        }
    );

District.php

#[ORM\OneToMany(mappedBy: 'district', targetEntity: Village::class)]
private Collection $villages;

public function __construct()
{
    $this->villages = new ArrayCollection();
}
....
public function getDistrictName(): ?string
{
    return $this->district_name;
}

public function setDistrictName(string $district_name): static
{
    $this->district_name = $district_name;

    return $this;
}

/**
 * @return Collection<int, Village>
 */
public function getVillages(): Collection
{
    return $this->villages;
}

Village.php

#[ORM\ManyToOne(inversedBy: 'villages')]
private ?district $district = null;

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

public function getVillageName(): ?string
{
    return $this->village_name;
}

public function setVillageName(string $village_name): static
{
    $this->village_name = $village_name;

    return $this;
}

public function getDistrict(): ?district
{
    return $this->district;
}

public function setDistrict(?district $district): static
{
    $this->district = $district;

    return $this;
}

控制器

#[Route('/admin/add-product', name: 'add-products', methods: ['GET','POST'])]
public function addProducts(Request $request): Response
{
    $product = new Product();
    $form = $this->createForm(ProductType::class, $product);
    $form->handleRequest($request);
    return $this->render('admin/index.html.twig', [
        'form' => $form,
    ]);
}

产品

<?php

    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
    #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private ?int $id = null;
    
        #[ORM\Column(length: 255)]
        private ?string $product_name = null;
    
        #[ORM\Column(length: 255)]
        private ?string $size = null;
    
        #[ORM\Column(length: 255)]
        private ?string $unit = null;
    
        #[ORM\Column(length: 255)]
        private ?string $district = null;
    
        #[ORM\Column(length: 255)]
        private ?string $village = null;
    
        #[ORM\Column(length: 255)]
        private ?string $date = null;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        public function getProductName(): ?string
        {
            return $this->product_name;
        }
    
        public function setProductName(string $product_name): static
        {
            $this->product_name = $product_name;
    
            return $this;
        }
    
        public function getSize(): ?string
        {
            return $this->size;
        }
    
        public function setSize(string $size): static
        {
            $this->size = $size;
    
            return $this;
        }
    
        public function getUnit(): ?string
        {
            return $this->unit;
        }
    
        public function setUnit(string $unit): static
        {
            $this->unit = $unit;
    
            return $this;
        }
    
        public function getDistrict(): ?string
        {
            return $this->district;
        }
    
        public function setDistrict(string $district): static
        {
            $this->district = $district;
    
            return $this;
        }
    
        public function getVillage(): ?string
        {
            return $this->village;
        }
    
        public function setVillage(string $village): static
        {
            $this->village = $village;
    
            return $this;
        }
    
        public function getDate(): ?string
        {
            return $this->date;
        }
    
        public function setDate(string $date): static
        {
            $this->date = $date;
    
            return $this;
        }
    }
tktrz96b

tktrz96b1#

您遇到此错误是因为您的表单绑定到Product实体,其中包含变量$district作为字符串类型属性。因此,在district表单字段中所做的任何更改都会自动与实体中的相应属性同步。
我可以提出两个选择来解决这种情况:

直接实体关系:可以考虑在村庄实体和您的产品实体之间建立直接关系,这样可以定义两者之间的关联,方便您管理和访问产品相关的村庄信息。
**新建Village的FormType:**或者,您可以专门为Village实体创建一个专用的FormType。此FormType将定义用于捕获与Village相关的数据的结构和验证规则。然后,您可以在现有的Product表单中添加一个使用Village FormType的字段。当您希望将特定于Village的表单逻辑与Product表单分开封装时,此方法非常有用。

相关问题