微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Easyadmin 3 在 AssociationField 如何使多对多行为像多对一

如何解决Easyadmin 3 在 AssociationField 如何使多对多行为像多对一

在我的表单中,我有一个多对多关系的 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;
    }


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。