输出
App\Entity\TourPackage {#313 ▼ -id: null -tour_category: “文化旅游”-价格:“3400”-旅游概述:“”“
<div>tourPackageImage1</div> """ -images: Doctrine\Common\Collections \ ArrayCollection {#314 ▼ -elements: array:2 [▼ 0 => App\Entity \ Images {#630 ▼ -id: null -image_name: null -tourPackage: App\Entity \ TourPackage {#313} } 1 => App\Entity \ Images {#438 ▼ -id: null -image_name: "random_image.jpg" -tourPackage: App\Entity \ TourPackage {#313} } ] } -itinerary: Doctrine\Common\Collections \ ArrayCollection {#315 ▶} }
旅游套餐类型
->add('images',CollectionType::class,[
'entry_type' => ImageType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label' => false,
'by_reference' => false,
])
图像类型
$builder
->add('tour_image_1', FileType::class,[
'mapped' => false,
'required' => false
])
->add('tour_image_2', FileType::class,[
'mapped' => false,
'required' => false
])
->add('tour_image_3', FileType::class,[
'mapped' => false,
'required' => false
])
;
控制器
$tourPackageImage1 = $request->files->get('tour_package')['images']['__name__']['tour_image_1'];
$tourPackageImage2 = $request->files->get('tour_package')['images']['__name__']['tour_image_2'];
$tourPackageImage3 = $request->files->get('tour_package')['images']['__name__']['tour_image_3'];
$tour_images = [$tourPackageImage1, $tourPackageImage2, $tourPackageImage3];
$images = new Images();
$images->setImageName($uploadImage->uploadImage($tourPackageImage1));
$tourPackage->addImage($images);
dd($tourPackage);
当 dd($tourPackage) 时,第一个数组为空。我什至试图让条件不起作用。如何解决这个问题?
0 => App\Entity \ Images {#630 ▼
-id: null
-image_name: null
-tourPackage: App\Entity \ TourPackage {#313}
}
1 => App\Entity \ Images {#438 ▼
-id: null
-image_name: "random_image.jpg"
-tourPackage: App\Entity \ TourPackage {#313}
}
如果上传 3 张图片,则第 1 张为空,在空数组后添加 3 张图片。如何删除空数组?
我建议:
$tourPackageValid = array_filter($tourPackage, function ($item){
return $item->getImageName() !== null;
});