0
我正在尝试使用FOSUserBundle将配置文件图片添加到我的用户实体的symfony2项目中。我已经与symfony2文件上传过程合作,当我使用FOSUserBundle时,我只有上传和数据库存在问题。Symfony2 FOSUserBundle用户图片上传
这里是我的实体:
namespace ICGM2\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
/* * *************************************************************** */
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
$this->path = uniqid('', true) . '.' . $this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file == $this->getAbsolutePath()) {
unlink($file);
}
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir() . '/' .
$this->id . '/' . $this->path;
}
protected function getUploadRootDir()
{
return __DIR__ . '/../../../../web/' . $this->getUploadDir() . '/' . $this->id;
}
protected function getUploadDir()
{
return 'uploads/users';
}
/**
* Set path
*
* @param string $path
* @return User
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
....
和表单:UserType.php
$builder->add('file', 'file', array(
'required' => false
));