Symfony 5.2 select2 如何加载保存的实体来选择2 关系 多对多

Symfony 5.2 select2 How to loading saved entities to select2 relationship Many to Many

提问人:DODO 提问时间:11/12/2023 更新时间:11/12/2023 访问量:17

问:

我有两个实体 CustomSupplier,BlockedPn 与 ManyToMany 关系。 我正在使用 select2 从 CustomSupplier 实体中选择值。

当我从$suppliers中保存或删除值时,它可以正常工作。

但是我对详细选择的保存值有一些问题。 所选值未加载以选择输入。

我在 FormType 中尝试了不同的设置,但没有任何帮助。

我必须手动加载所选值吗?阿克

实体被阻止Pn.php

    class BlockedPn
    {
        /**
         * @ORM\ManyToMany(targetEntity=CustomSupplier::class, inversedBy="blockedPns")
         */
        private $suppliers;
    
        public function __construct()
        {
            $this->suppliers = new ArrayCollection();
        }
    
        /**
         * @return Collection|CustomSupplier[]
         */
        public function getSuppliers(): Collection
        {
            return $this->suppliers;
        }
    
        public function addSuppliers(CustomSupplier $supplier): self
        {
            if (!$this->suppliers->contains($supplier)) {
                $this->suppliers[] = $supplier;
            }
    
            return $this;
        }
    
        /**
         * @param mixed $suppliers
         */
        public function setSuppliers($suppliers)
        {
            $this->suppliers = $suppliers;
        }
    ...
    }
    
    
    Entity CustomSupplier.php
    
    class CustomSupplier
    {
    
    /**
         * @ORM\ManyToMany(targetEntity=BlockedPn::class, mappedBy="suppliers")
         */
        private $blockedPns;
    
        public function __construct()
        {
            $this->blockedPns = new ArrayCollection();
        }
    
        /**
         * @return Collection|CustomSupplier[]
         */
        public function getBlockedPns(): Collection
        {
            return $this->blockedPns;
        }

    /**
     * @param $pn
     */
    public function addBlockedPns($pn)
    {
        $this->blockedPns[] = $pn;
    }
}

我的 FormType 和 EntityType 具有多个选项。

class BlockedPnType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add("pn",null,[
                'label' => 'Part Number'
            ])
            ->add("suppliers",EntityType::class,[
                'class' => CustomSupplier::class,
                'choice_label' => 'name',
                'choice_value' => 'id',
                'multiple' => true,
                'mapped' => true,
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('b')->orderBy('b.name', 'ASC');
                }
}
}

控制器操作详情:

/**
 * @Route ("/blocked-pn/detail/{id}",name="block_pns_detail")
 */
public function blockPnsDetail(int $id, Request $request)
{
    $blockedPn = $this->em->getRepository(BlockedPn::class)
                          ->find($id);
    

    $form = $this->createForm(BlockedPnType::class, $blockedPn);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
    {
        $this->addFlash('success','Part number edited');

        $this->em->persist($blockedPn);
        $this->em->flush();

        return $this->redirectToRoute('block_pns_detail',['id' => $blockedPn->getId()]);
    }


    return $this->render('blocked_pns/new.html.twig',['form' => $form->createView()]);
}

从模板 .twig

<script>
   $(document).ready(function() {
        $('#blocked_pn_suppliers').select2();
    });
</script>


                        <div class="row">
                            <div class="col-md-6 col-12">
                                    <label for="term_name">{{ form_label(form.suppliers) }}</label>
                                        {{ form_widget(form.suppliers) }}
                                    <div class="text-danger">{{ form_errors(form.suppliers) }}</div>
                            </div>

                        </div>
php symfony 实体 symfony5

评论


答: 暂无答案