我有一个包含集合的Symfony表单,其定义如下:

<?php declare(strict_types=1);

namespace App\Form;

use App\Entity\Documents;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DocumentsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'documents',
            CollectionType::class,
            [
                'entry_type' => DocumentType::class,
                'by_reference' => false,
                'entry_options' => [
                    'label' => false,
                ],
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'attr' => [
                    'class' => 'documents-collection',
                    'data-min-items' => 1,
                ],
                'required' => true,
            ]
        );

        parent::buildForm($builder, $options);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => Documents::class,
            ]
        );
    }
}

和DocumentType如下:

<?php declare(strict_types=1);

namespace App\Form;

use App\Entity\Document;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DocumentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'description',
                TextType::class,
                [
                    'required' => true,
                    'attr' => [
                        'placeholder' => 'Document description, eg: Ticket, receipt, itinerary, map, etc…',
                    ],
                ]
            )
            ->add(
                'document',
                FileType::class,
                [
                    'mapped' => false,
                    'required' => true,
                ]
            );

        parent::buildForm($builder, $options);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => Document::class,
            ]
        );
    }
}

文件实体是:

<?php declare(strict_types=1);

namespace App\Entity;

use App\Service\Uuid;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Documents
{
    /**
    * @ORM\Column(type="uuid")
    * @ORM\GeneratedValue(strategy="UUID")
    * @ORM\Id
    */
    private $id;

    /**
    * @ORM\ManyToMany(
    *     targetEntity="Document",
    *     cascade={"persist", "remove"},
    *     orphanRemoval=true
    * )
    * @ORM\JoinTable(
    *     name="documents_document",
    *     joinColumns={
    *         @ORM\JoinColumn(name="documents_id", referencedColumnName="id"),
    *     },
    *     inverseJoinColumns={
    *         @ORM\JoinColumn(name="document_id", referencedColumnName="id", unique=true),
    *     }
    * )
    * @var Document[]
    */
    private $documents;


    public function __construct()
    {
        $this->id = Uuid::uuid4();

        $this->documents = new ArrayCollection();
    }


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

    /**
    * @return Collection
    */
    public function getDocuments(): Collection
    {
        return $this->documents;
    }

    /**
    * @param Document $document
    *
    * @return $this
    */
    public function addDocument(Document $document): Documents
    {
        if (!$this->documents->contains($document)) {
            $this->documents->add($document);
            $document->setDocuments($this);
        }

        return $this;
    }

    /**
    * @param Document $document
    *
    * @return bool
    */
    public function hasDocument(Document $document): bool
    {
        return $this->documents->contains($document);
    }

    /**
    * @param Document $document
    *
    * @return $this
    */
    public function removeDocument(Document $document): Documents
    {
        if ($this->documents->contains($document)) {
            $this->documents->removeElement($document);
        }

        return $this;
    }

    /**
    * @param Collection $documents
    *
    * @return $this
    */
    public function setDocuments(Collection $documents): Documents
    {
        $this->documents = $documents;

        return $this;
    }

    /**
    * @return $this
    */
    public function clearDocuments(): Documents
    {
        $this->documents = new ArrayCollection();

        return $this;
    }
}

文件实体是:

<?php declare(strict_types=1);

namespace App\Entity;

use App\Service\Uuid;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Document
{
    /**
    * @var Uuid|string
    * @ORM\Column(type="uuid")
    * @ORM\GeneratedValue(strategy="UUID")
    * @ORM\Id
    */
    private $id;

    /**
    * @var Documents
    * @ORM\ManyToOne(targetEntity="Documents")
    */
    private $documents;

    /**
    * @var string
    * @ORM\Column(type="string", length=1024, nullable=false)
    */
    private $description;


    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }


    /**
    * @return Uuid|string
    */
    public function getId()
    {
        return $this->id;
    }

    /**
    * @return Documents
    */
    public function getDocuments(): Documents
    {
        return $this->documents;
    }

    /**
    * @param Documents $documents
    *
    * @return $this
    */
    public function setDocuments(Documents $documents): Document
    {
        $this->documents = $documents;

        return $this;
    }

    /**
    * @return string
    */
    public function getDescription(): ?string
    {
        return $this->description;
    }

    /**
    * @param string $description
    *
    * @return $this
    */
    public function setDescription(string $description): Document
    {
        $this->description = $description;

        return $this;
    }
}

我在我的控制器中创建了这样的表单:

$repo = $entityManager->getRepository(Documents::class);
$documents = $repo->findOneBy(['id' => $id]);

$form = $this->formFactory->create(
    DocumentsType::class,
    $documents
);

当我在呈现的表单中向集合添加新的Document条目然后保存表单时,它们被正确地持久保存到数据库并链接到Documents实体 .

如果我删除集合中的最后一个条目,它将从$ documents集合中正确删除,然后从文档表中删除,因为不再有任何引用它 .

但是,如果我删除集合中间的条目,则Doctrine将保留已删除的一个及其关注者的剩余条目中的数据,然后删除列表中的最后一个实体,更改所有实体的ID .

我使用UUID作为新文件名保存在 DocumentType 上的 document 字段中上传的文件,因此从集合中删除条目时,ID必须保持不变 . 我已经尝试将映射和未映射的id字段添加到集合中,但是未映射的字段将被完全忽略,并且映射的字段将允许用户修改id列中的数据,因此不适合在此处使用 .

我需要做些什么才能修改此表单以使Doctrine维护集合中的数据与它在数据库中表示的实体之间的连接?