在 Symfony 3 上使用 VichUploaderBundle 进行“多次上传”
2018-05-17
8615
我尝试使用 VichUploader 包允许多次上传。在项目中,我有一个 Theater 类,它拥有一个主图像,但也拥有多个次图像(图像集合)。实际上,每个次图像都是一个资源。 因此,一个剧院有一对多资源,并且一个资源连接到一个剧院。
但是,当我尝试创建时,我可以访问我的表单,但是当我尝试保存时出现错误:
Expected argument of type "AppBundle\Entity\Resources", "AppBundle\Entity\Theatre" given
这是我的剧院类,仅包含多个上传的详细信息:
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Theatre
*
* @ORM\Table(name="theatre")
* @ORM\Entity
* @Vich\Uploadable
*/
class Theatre
{
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Resources", mappedBy="theatre", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $images;
// ..
/**
* Constructor
*/
public function __construct()
{
$this->images = new \Doctrine\Common\Collections\ArrayCollection();
}
// MultiUpload
/**
* @return ArrayCollection
*/
public function getImages()
{
return $this->images;
}
/**
* @param ArrayCollection $pictures
*/
public function setImages($pictures)
{
$this->images = $pictures;
}
public function getAttachImages()
{
return null;
}
/**
* @param array $files
*
* @return array
*/
public function setAttachImages(array $files=array())
{
if (!$files) return [];
foreach ($files as $file) {
if (!$file) return [];
$this->attachImages($file);
}
return [];
}
/**
* @param UploadedFile|null $file
*/
public function attachImages(UploadedFile $file=null)
{
if (!$file) {
return;
}
$picture = new Resources();
$picture->setImage($file);
$this->addImage($picture);
}
/**
* Add image.
*
* @param \AppBundle\Entity\Resources $image
*
*
*/
public function addImage(\AppBundle\Entity\Resources $image)
{
$image->setTheatre($this);
//$this->images->add($image);
$this->images[] = $image;
//return $this;
}
/**
* Remove image.
*
* @param \AppBundle\Entity\Resources $image
*
*
*/
public function removeImage(\AppBundle\Entity\Resources $image)
{
$image->setTheatre(null);
$this->images->removeElement($image);
// return $this->images->removeElement($image);
}
然后是我的类资源:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Resources
*
* @ORM\Table(name="resources")
* @ORM\Entity
* @Vich\Uploadable
*/
class Resources
{
/**
* @var Theatre
* @ORM\ManyToOne(targetEntity="Theatre", inversedBy="images")
*/
private $theatre;
/**
* @Vich\UploadableField(mapping="uploads_image", fileNameProperty="url")
* @Assert\File(
* mimeTypes = {"image/png", "image/jpeg", "image/jpg"},
* mimeTypesMessage = "Please upload a valid valid IMAGE"
* )
*
*
* @var File $image
*/
protected $image;
/**
* @ORM\Column(type="string", length=255, name="url")
*
* @var array $url
*/
protected $url;
/**
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return Theatre
*/
public function setImage(File $image = null)
{
$this->image = $image;
}
public function getImage()
{
return $this->image;
}
// ..
/**
* Set theatre.
*
* @param \AppBundle\Entity\Theatre $theatre
*
* @return Resources
*/
public function setTheatre(\AppBundle\Entity\Theatre $theatre)
{
$this->theatre = $theatre;
return $this;
}
/**
* Get theatre.
*
* @return \AppBundle\Entity\Theatre
*/
public function getTheatre()
{
return $this->theatre;
}
/**
* Set url.
*
* @param string $url
*
* @return Resources
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url.
*
* @return array
*/
public function getUrl()
{
return $this->url;
}
然后我添加到构建器:
namespace AppBundle\Form\Collection;
use AppBundle\Entity\Theatre;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Vich\UploaderBundle\Form\Type\VichFileType;
class TheatreImages extends AbstractType{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('attachImages', FileType::class, ['multiple'=>true, 'required'=>false])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Theatre::class,
));
}
/**
* @return string
*/
public function getName()
{
return 'app_theatreImages';
}
}
我使用捆绑包 easyAdminBundle 添加我的配置:
easy_admin:
entities:
Theatre:
class: AppBundle\Entity\Theatre
list:
title: 'Liste des théâtres'
fields:
- 'Name'
- 'adress'
- 'Metro station'
- { property: 'Main image', type: 'image', template: 'theatreFile.html.twig', base_path: '%app.path.theatre_images%' }
new:
title: 'Création théâtre'
fields:
- { type: 'section', label: 'Information du théâtre' }
- {property: 'name', label: 'Nom'}
- {property: 'number_of_seats', type: 'integer', label: 'Nombre de sièges'}
- {property: 'about', label: 'description'}
- { property: 'imageFile', type: 'vich_file', label: 'image', type_options: { required: false}}
- {property: 'images', type: 'collection', type_options: {entry_type: 'AppBundle\Form\Collection\TheatreImages', by_reference: false}}
- { type: 'section', label: 'Localisation du théâtre' }
- {property: 'adress', label: 'adresse'}
- {property: 'metro_station', label: 'Station de métro'}
- {property: 'location_coordinates', label: 'Coordonnées'}
edit:
title: "Édition théâtre"
fields:
- { type: 'section', label: 'Information du théâtre' }
- {property: 'name', label: 'Nom'}
- {property: 'number_of_seats', type: 'integer', label: 'Nombre de sièges'}
- {property: 'about', label: 'description'}
- { property: 'imageFile', type: 'vich_file', label: 'image', type_options: { required: false}}
- {property: 'images', type: 'collection', type_options: {entry_type: 'AppBundle\Form\Collection\TheatreImages', by_reference: false}}
- { type: 'section', label: 'Localisation du théâtre' }
- {property: 'adress', label: 'adresse'}
- {property: 'metro_station', label: 'Station de métro'}
- {property: 'location_coordinates', label: 'Coordonnées'}
提前致谢。
2个回答
我为一个非常有用的软件包 [VichUploader] 创建了一个解决方案,它缺少多个上传功能,并且适用于我在 [Symfony] 5.2 上创建的每个 Symfony 版本。
它基于 OneToMany 关系,并且运行良好。因此,我在自定义表单中使用了 CollectionType 和 [VichFileType],并在控制器中使用了一些小技巧,这里是代码,要查看整个项目,您可以在我的
GitHub
中找到它
[链接]
https://github.com/malek-laatiri
Admission.php
class Admission
{
/**
* @ORM\OneToMany(targetEntity=Diplome::class, mappedBy="admission")
*/
private $diplomes;
/**
* @return Collection|Diplome[]
*/
public function getDiplomes(): Collection
{
return $this->diplomes;
}
public function addDiplome(Diplome $diplome): self
{
if (!$this->diplomes->contains($diplome)) {
$this->diplomes[] = $diplome;
$diplome->setAdmission($this);
}
return $this;
}
public function removeDiplome(Diplome $diplome): self
{
if ($this->diplomes->removeElement($diplome)) {
// set the owning side to null (unless already changed)
if ($diplome->getAdmission() === $this) {
$diplome->setAdmission(null);
}
}
return $this;
}
}
Diplome.php
<?php
namespace App\Entity;
use App\Repository\DiplomeRepository;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=DiplomeRepository::class)
* @Vich\Uploadable
*/
class Diplome
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Admission::class, inversedBy="diplomes",cascade={"persist","remove"})
*/
private $admission;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @Vich\UploadableField(mapping="product_image", fileNameProperty="name")
* @var File
*/
private $file;
public function getId(): ?int
{
return $this->id;
}
public function getAdmission(): ?Admission
{
return $this->admission;
}
public function setAdmission(?Admission $admission): self
{
$this->admission = $admission;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getFile()
{
return $this->file;
}
public function setFile( $file)
{
$this->file = $file;
return $this;
}
}
AdmissionType.php
<?php
namespace App\Form;
use App\Entity\Admission;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class Admission1Type extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('diplomes', CollectionType::class, [
'entry_type' => DiplomeType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'label'=>false,
'by_reference' => false,
'disabled' => false,
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Admission::class,
]);
}
}
DiplomeType.php
<?php
namespace App\Form;
use App\Entity\Diplome;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class DiplomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file',VichFileType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Diplome::class,
"allow_extra_fields" => true,
]);
}
}
_form.html.twig
<ul id="diplomes-fields-list"
data-prototype="{{ form_widget(form.diplomes.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}"
data-widget-counter="{{ form.diplomes|length }}">
{% for emailField in form.diplomes %}
<li>
{{ form_errors(emailField) }}
{{ form_widget(emailField) }}
</li>
{% endfor %}
</ul>
<button type="button"
class="add-another-collection"
data-list-selector="#diplomes-fields-list">Add another email
</button>
script.js
jQuery(document).ready(function () {
jQuery('.add-another-collection').click(function (e) {
var list = $("#diplomes-fields-list");
var counter = list.data('widget-counter') | list.children().length;
var newWidget = list.attr('data-prototype');
newWidget = newWidget.replace(/__name__/g, counter);
counter++;
list.data('widget-counter', counter);
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
newElem.append('<a href="#" class="remove-tag" style="color: darkred">remove</a>');
$('.remove-tag').click(function(e) {
e.preventDefault();
$(this).parent().remove();
});
});
});
Controller.php
$admission = new Admission();
$form = $this->createForm(Admission1Type::class, $admission);
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
if ($form->isSubmitted() && $form->isValid()) {
foreach ($form->getData()->getNotes() as $dip){
$entityManager->persist($dip);
$admission->addNote($dip);
}
$entityManager->persist($admission);
$entityManager->flush();
Vinci
2021-03-27
Vihcuploader 不能很好地支持多个文件上传,您必须创建自定义表单类型来处理它,也许这个解决方案会有所帮助:
Jasson Rojas
2018-05-21