你好我做的例子link我使用FosUserBundle扩展它,所以任何登录用户都与文件表有关系。但我不想去多文件上传。我查了一些关于它的帖子但是不能让这个工作。我发现有人认为要创建自定义验证器link post here。我的代码仍然没有工作,所以我请求帮助如何更改我的文件以使其工作。
File.php
<?php
namespace Felek\FosBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Entity\File
*
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="file", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})})
*/
class File
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="text")
*/
private $path;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
private $creationDate;
/**
* @var integer
* @ORM\Column(type="integer")
*/
private $author;
/**
* @var string
* @ORM\Column(type="text")
*/
private $orginalName;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
* @return File
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set creationDate
*
* @param \DateTime $creationDate
* @return File
* @ORM\PrePersist()
*/
public function setCreationDate()
{
$this->creationDate = new \DateTime();
return $this;
}
/**
* Get creationDate
*
* @return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set author
*
* @param integer $author
* @return File
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return integer
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set orginalName
*
* @param string $orginalName
* @return File
*/
// public function setOrginalName($orginalName)
// {
// $this->orginalName = $orginalName;
//
// return $this;
// }
public function setOrginalName($orginalName)
{
// $orginalName = $this->getPath();
$this->orginalName = $orginalName;
return $this;
}
/**
* Get orginalName
*
* @return string
*/
public function getOrginalName()
{
return $this->orginalName;
}
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="File")
* @ORM\JoinColumn(name="author", referencedColumnName="id", nullable=false)
*/
protected $User;
/**
* Set User entity (many to one).
*
* @param \Entity\User $User
* @return \Entity\File
*/
public function setUser(User $User = null)
{
$this->User = $User;
return $this;
}
/**
* Get User entity (many to one).
*
* @return \Entity\File
*/
public function getUser()
{
return $this->User;
}
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
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/files/'.$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/documents';
}
private $temp;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename.'.'.$this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
FileType
<?php
namespace Felek\FosBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FileType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ->add('path')
->add('file','file',array(
"attr" => array(
"multiple" => true
)
)
)
// ->add('creationDate')
// ->add('author')
// ->add('orginalName')
// ->add('User')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Felek\FosBundle\Entity\File',
));
}
/**
* @return string
*/
public function getName()
{
return 'felek_fosbundle_file';
}
}
和控制器
public function createAction(Request $request)
{
$entity = new File();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
//przypisanie zalogowanego uzytkownika
$entity->setUser(
$this->get('security.context')->getToken()->getUser()
);
// $file = $form['file']->getData();
$entity->setOrginalName($form['file']->getData()->getClientOriginalName());//pobranie z formularza nazwy pliku i przypsanie go do bazy danych
// print_r($entity);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('file_show', array('id' => $entity->getId())));
}
return $this->render('FelekFosBundle:File:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
那么任何建议如何改变它?我认为很好的解决方案是将foreach文件从传递到实体并处理它,仍然不知道我该怎么办....