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
));