0

在我的表单中,我有一个多对多关系的 AssociationField,我希望用户只选择一个选项

这些选项涉及存储我的 Product 实体的 productCategories 字段的 AssociationField 的变量

ProductCrudController 的领域



    public function configureFields(string $pageName): iterable
    {

        $subproductCategories = AssociationField::new('productCategories', 'subcategory')
            ->setFormTypeOption('query_builder', function (ProductCategoryRepository $productCategoryRepository) {
                return $productCategoryRepository->createQueryBuilder('pc')
                            ->where('pc.parent IS NOT NULL');
            });

        return [
            
            $subproductCategories
            
        ];
    }

}

产品实体领域


    /**
     * @ORM\ManyToMany(targetEntity=ProductCategory::class, inversedBy="products")
     * @Groups("api_product_browse")
     * @Assert\NotBlank
     */
    private $productCategories;


    /**
     * @return Collection|ProductCategory[]
     */
    public function getProductCategories(): Collection
    {
        return $this->productCategories;
    }

    public function addProductCategory(ProductCategory $productCategory): self
    {
        if (!$this->productCategories->contains($productCategory)) {
            $this->productCategories[] = $productCategory;
        }

        return $this;
    }

    public function removeProductCategory(ProductCategory $productCategory): self
    {
        $this->productCategories->removeElement($productCategory);

        return $this;
    }


4

0 回答 0