所以我有一个文档实体,用于存储在整个项目博客中使用的文件上传(目前只有图像)。现在,对于这篇文章,我希望能够上传并选择一个基本上除文件大小之外几乎没有限制的图像,但是对于一个类别我想只能使用正方形或不是横向而不是纵向的图像。
文档实体看起来像这样
class Document
{
/**
* @var integer
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $path;
private $webPath;
private $filename;
/**
* 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 setFullFilename()
{
$this->filename = $this->id . '.' . $this->path;
}
public function getFilename()
{
return $this->filename;
}
public function getAbsolutePath()
{
//return null === $this->path ? null : $this->getUploadRootDir(). '/' . $this->path;
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir() . '/';
}
public function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
public function getUploadDir()
{
return 'bundles/pgblog/images/uploads';
}
private $file;
private $temp;
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
if(is_file($this->getAbsolutePath()))
{
$this->temp = $this->getAbsolutePath();
}
else {
$this->path = 'initial';
}
}
public function getFile()
{
return $this->file;
}
public function preUpload()
{
if(null !== $this->getFile())
{
$this->path = $this->getFile()->guessExtension();
$this->setMimetype();
$this->setSize();
/*
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
*/
}
}
public function upload()
{
if(null === $this->getFile())
{
return;
}
if(isset($this->temp))
{
unlink($this->temp);
$this->temp = null;
}
$this->getFile()->move($this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension());
$this->file = null;
}
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
}
public function removeUpload()
{
if(isset($this->temp))
{
unlink($this->temp);
}
}
public function __toString()
{
return $this->name;
}
/**
* @var string
*/
private $mimetype;
/**
* Set mimetype
*
* @param string $mimetype
* @return Document
*/
public function setMimetype()
{
$this->mimetype = $this->getFile()->getMimeType();
return $this;
}
/**
* Get mimetype
*
* @return string
*/
public function getMimetype()
{
return $this->mimetype;
}
/**
* @var integer
*/
private $size;
/**
* Set size
*
* @param integer $size
* @return Document
*/
public function setSize()
{
$this->size = $this->getFile()->getSize();
return $this;
}
/**
* Get size
*
* @return integer
*/
public function getSize()
{
return $this->size;
}
}
我使用单独的表单进行文件上传和文章/类别创建。创建文章或类别时,可以从当前所有文件列表
中选择文件这里是文章的表单类型
class ArticleType extends AbstractType
{
...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('text')
->add('tags')
->add('category')
->add('image')
;
}
...
}
和文章实体
class Article
{
...
/**
* Set image
*
* @param \Acme\UtilBundle\Entity\Document $image
* @return Article
*/
public function setImage(\PG\BlogBundle\Entity\Document $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return \Acme\BlogBundle\Entity\Document
*/
public function getImage()
{
return $this->image;
}
}
Article
通过单向多对一关系与Document
相关
manyToOne:
image:
targetEntity: Document
joinColumn:
name: document_id
referencedColumnName: id
因此在表单中可以通过select设置文件。对于这篇文章,这很好,因为我希望能够使用任何图像作为附件,但对于类别我只想要像前面所述的方形文件。 我考虑过对类别图像使用验证,但由于图像是由select选择的,因此实际数据只是一个字符串(上传表单中给出的文件名)而不是自身的图像,因此验证会返回错误< / p>
Catchable Fatal Error: Argument 1 passed to Acme\BlogBundle\Entity\Category::setImage() must be an instance of Acme\BlogBundle\Entity\Document, string given...
所以我的问题是,如何将类别表单中的图像选项限制为仅方形图像,如何正确验证?
我只想使用方形图像作为类别的原因是,顺便说一下,我可以显示一个很好的对称列表。
提前致谢!
答案 0 :(得分:0)
将图像尺寸存储在文档实体中(长度,宽度)。
这样,在获取类别表单的文档集时,您可以过滤长度==宽度的图像文档,因此只显示相应的文档。
对于验证,您有多种选择,最好的起点是here。在你的位置,我会调查validation groups。