带有arrayCollection的Symfony表单EntityType不会显示每个实体

时间:2018-09-11 13:10:13

标签: forms symfony-3.4 arraycollection

在我的应用程序中,我有一个实体ZoneMaterial,该实体将Zone链接到一组其他实体。 ZoneMaterial实体有两个arrayCollections: epis epcs ,它们是其他实体。我对这两个收藏有疑问。

EPISEPCS链接和取消链接到我的ZoneMaterial并不是问题,但是当我从{{1}中删除EPIEPC时}},下一次进入更新视图以再次链接时,我将找不到它。

ZoneMaterial实体的配置如下:

ZoneMaterial

我的表单定义如下:

class ZoneMaterial
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Zone", inversedBy="zoneMaterials")
     * @ORM\JoinColumn(name="zone_id", referencedColumnName="id")
     */
    private $zone;

    /**
     * @ORM\ManyToOne(targetEntity="Material")
     * @ORM\JoinColumn(name="material_id", referencedColumnName="id")
     */
    private $material;

    /**
     * @ORM\ManyToOne(targetEntity="Outil")
     * @ORM\JoinColumn(name="outil_id", referencedColumnName="id")
     */
    private $outil;

    /**
     * @ORM\ManyToOne(targetEntity="Process")
     * @ORM\JoinColumn(name="process_id", referencedColumnName="id")
     */
    private $process;

    /**
     * @ORM\ManyToOne(targetEntity="Method")
     * @ORM\JoinColumn(name="method_id", referencedColumnName="id")
     */
    private $method;

    /**
     * @ORM\ManyToOne(targetEntity="DustLevel")
     * @ORM\JoinColumn(name="dustLevel_id", referencedColumnName="id")
     */
    private $dustLevel;

    /**
     * @ORM\ManyToMany(targetEntity="EPC", cascade={"persist"}, orphanRemoval=true)
     * @ORM\JoinTable(name="app_zone_material_epc",
     *      joinColumns={@ORM\JoinColumn(name="zoneMaterial_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="epc_id", referencedColumnName="id")}
     *      )
     */
    private $epcs;

    /**
     * @ORM\ManyToMany(targetEntity="EPI", cascade={"persist"}, orphanRemoval=true)
     * @ORM\JoinTable(name="app_zone_material_epi",
     *      joinColumns={@ORM\JoinColumn(name="zoneMaterial_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="epi_id", referencedColumnName="id")}
     *      )
     */
    private $epis;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="create")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="update")
     */
    private $updatedAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $deletedAt;

    public function __construct(Zone $zone){
        $this->epcs    = new ArrayCollection();
        $this->epis    = new ArrayCollection();
        $this->setZone($zone);
        $this->createdAt            = new \DateTime('now');
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    public function getZone()
    {
        return $this->zone;
    }

    public function getMaterial()
    {
        return $this->material;
    }

    public function getProcess()
    {
        return $this->process;
    }

    public function getOutil()
    {
        return $this->outil;
    }

    public function setOutil($outil)
    {
        $this->outil = $outil;
    }

    public function getMethod()
    {
        return $this->method;
    }

    public function getDustLevel()
    {
        return $this->dustLevel;
    }

    public function setZone($zone)
    {
        $this->zone = $zone;
    }

    public function setMaterial($material)
    {
        $this->material = $material;
    }

    public function setProcess($process)
    {
        $this->process = $process;
    }

    public function setMethod($method)
    {
        $this->method = $method;
    }

    public function setDustLevel($dustLevel)
    {
        $this->dustLevel = $dustLevel;
    }

    public function addEpc(EPC $e)
    {
        $this->epcs[] = $e;
    }

    public function removeEpc(EPC $e)
    {
        $this->epcs->removeElement($e);
    }

    public function getEpcs()
    {
        return $this->epcs;
    }

    public function addEpi(EPI $e)
    {
        $this->epis[] = $e;
    }

    public function removeEpi(EPI $e)
    {
        $this->epis->removeElement($e);
    }

    public function getEpis()
    {
        return $this->epis;
    }
}

并显示为:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('process', EntityType::class ,    array(  'label'               =>'Processus',
                                                    'choices_as_values'   => true,
                                                    'class'               => 'AppBundle\Entity\Process',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    }))
    ->add('material', EntityType::class ,   array(  'label'               =>'Matériau',
                                                    'choices_as_values'   => true,
                                                    'class'               => 'AppBundle\Entity\Material',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    }))
    ->add('method', EntityType::class ,     array(  'label'               =>'Méthode',
                                                    'choices_as_values'   => true,
                                                    'class'               => 'AppBundle\Entity\Method',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    }))
    ->add('outil', EntityType::class ,     array(   'label'                =>'Outil',
                                                    'choices_as_values'   => true,
                                                    'class'               => 'AppBundle\Entity\Outil',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    }))
    ->add('dustLevel', EntityType::class ,  array(  'label'               => "Niveau d'empoussièrement",
                                                    'choices_as_values'   => true,
                                                    'class'               => 'AppBundle\Entity\DustLevel',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    }))
    ->add('epcs', EntityType::class,        array(  'label'               =>'MPC',
                                                    'class'               => 'AppBundle\Entity\EPC',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    },
                                                    'multiple'            => true,
                                                    'expanded'            => true))
    ->add('epis', EntityType::class,        array(  'label'               =>'EPI',
                                                    'class'               => 'AppBundle\Entity\EPI',
                                                    'query_builder'       =>  function ($repository) use ($options) {
                                                    return $repository
                                                        ->createQueryBuilder('a')
                                                        ->orderBy('a.name', 'ASC');
                                                    },
                                                    'multiple'            => true,
                                                    'expanded'            => true))

    ;
}

一切似乎都可以正常工作,但是当我从{{ form_start(form, {'action': path('updt_zone_material', {'id':id}), 'method': 'POST'}) }} <div class="modal-header"> <h4 class="modal-title" id="smallModalLabel">Matériau lié à la zone</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-sm-12"> {{ form_widget(form) }} </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">Fermer</button> <button type="submit" class="btn btn-primary waves-effect">Modifier</button> </div> {{ form_end(form) }} 删除EPCEPI时,我不能放回去,因为它只是从EPI或EPC列表中消失了以我的形式。

这里的表单没有我的元素选择过滤器:

ZoneMaterial

为什么每次我使用表单从实体中删除元素时,都不会再在表单中找到该元素以将其放回实体中?

这是控制器更新功能:

->add('epcs', EntityType::class, array(
'label'=>'MPC',
'class'=>'AppBundle\Entity\EPC',
'query_builder'=>feach time I remove an element from the entity using the form, I wont find it anymore in my formeach time I remove an element from the entity using the form, I wont find it anymore in my formunction ($repository) use ($options) {
return $repository->createQueryBuilder('a')->orderBy('a.name', 'ASC');
},
'multiple'=>true,

需要帮助

0 个答案:

没有答案