这是我的问题简介:我有2个与OneToMany关系关联的实体。第一次上传时没有问题,但是更新过程很混乱。如果有更新请求发送了 Form 的 handleRequest 函数,则删除了一些关系实体并添加了新的上传文件。
例如:我有1个积分和2个与该积分相关的额外文件。然后,当我想更新并上传其他文件时。执行Form的 handleRequest 功能时,最后一个文件已自动删除。
这是我的代码库:
信用实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\CreditRepository")
* @Vich\Uploadable()
*/
class Credit
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\ExtraFile", mappedBy="credit", cascade={"persist", "remove"})
* @var ExtraFile[] An ArrayCollection of ExtraFile objects.
*/
private $extraFiles;
ExtraFile 实体:
/**
* Class ExtraFile
* @ORM\Entity()
* @Vich\Uploadable()
*/
class ExtraFile
{
/**
* @ORM\ManyToOne(targetEntity="Credit", inversedBy="extraFiles")
*/
private $credit;
在我的控制器中:
public function applicationFiles(Credit $credit, Request $request): Response
{
$form = $this->createForm(CreditExtraType::class, $credit);
$form->handleRequest($request); // <--- At this point the $credit entity "extraFiles" entities changed and removed the last or two entities (it depent how much file send for upload) with OneToMany.
if ($form->isSubmitted() && $form->isValid()) {
以及 CreditExtraType 代码:
class CreditExtraType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('extraFiles', CollectionType::class, [
'entry_type' => CreditExtraVichFileType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'by_reference' => false,
'disabled' => false,
])
;
}
你有什么主意吗?