我上传了多个文件,但是我有这个错误:
类型" Symfony \ Component \ HttpFoundation \ File \ UploadedFile"," array"的预期参数给定
我使用DataTransformerInterface将内容输入文件转换为数组。
实体文件:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Document
*
* @ORM\Table(name="document")
* @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentRepository")
*/
class Document
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set name
*
* @param string $name
* @return Document
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path
*
* @param string $path
* @return Document
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
Code FileTransformer.php:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\DataTransformerInterface;
use AppBundle\Entity\Document;
use Doctrine\Common\Persistance\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileTransformer implements DataTransformerInterface {
/**
* @var EntityManagerInterface
*/
private $manager;
public function __construct(EntityManagerInterface $manager){
$this->manager = $manager;
}
public function transform($value) {
if ($value != "") {
return var_dump("error");
}
return $value;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads';
}
public function reverseTransform($files)
{
$Documents = [];
foreach($files as $file){
$Document = new Document();
$Document->setPath($file->getClientOriginalName());
$Document->setName($file->getClientOriginalName());
$this->manager->persist($Document);
$file->move(
$this->getUploadRootDir(),
$file->getClientOriginalName()
);
$this->manager->flush();
$Documents[] = $Document;
}
var_dump("2");
return $Documents;
}
}
Code DocumentType.php:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Form\FileTransformer;
use Doctrine\Common\Persistance\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;
use AppBundle\Entity\Document;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
class DocumentType extends AbstractType
{
/**
* @var EntityManagerInterface
*/
private $manager;
public function __construct(EntityManagerInterface $manager){
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name')->add('file', 'file',array('multiple' => 'multiple'));
$builder->get('file')->addModelTransformer(new FileTransformer($this->manager),true);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Document',
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_document';
}
}
Code DefualtController.php:
public function uploadAction(Request $request)
{
$document = new Document();
$form = $this->createForm(DocumentType::class, $document);
$form->handleRequest($request);
return $this->render('default/upload.html.twig',array('form'=>$form->createView()));
}