Symfony 在注册过程中上载配置文件图片-FOSUserBundle

Symfony 在注册过程中上载配置文件图片-FOSUserBundle,symfony,fosuserbundle,Symfony,Fosuserbundle,使用FOSUserBundle时,我在注册过程中遇到问题。在数据库中,它总是在path列下保存initial值,而不是我尝试上载的图像的实际路径,并且图像不会上载到我定义的目录中 名称空间会话\UserBundle\Entity; 使用FOS\UserBundle\Model\User作为基本用户; 使用条令\ORM\Mapping作为ORM; 使用Symfony\Component\Validator\Constraints作为断言; 使用Symfony\Component\HttpFoun

使用
FOSUserBundle
时,我在注册过程中遇到问题。在数据库中,它总是在
path
列下保存
initial
值,而不是我尝试上载的图像的实际路径,并且图像不会上载到我定义的目录中

名称空间会话\UserBundle\Entity;
使用FOS\UserBundle\Model\User作为基本用户;
使用条令\ORM\Mapping作为ORM;
使用Symfony\Component\Validator\Constraints作为断言;
使用Symfony\Component\HttpFoundation\File\UploadedFile;
使用Symfony\Component\Security\Core\Util\SecureRandom;
/**
*使用者
*
*@ORM\Table(name=“User”)
*@ORM\Entity(repositoryClass=“Session\UserBundle\Entity\UserRepository”)
*/
类用户扩展了BaseUser
{
/**
*@ORM\Id
*@ORM\Column(type=“integer”)
*@ORM\GeneratedValue(strategy=“AUTO”)
*/
受保护的$id;
/**
*@ORM\Column(type=“string”,length=255,nullable=true)
*/
公共道路;;
/**
*@Assert\File(maxSize=“6000000”)
*/
公共文件;
/*     * *************************************************************** */
/**
*@ORM\PostRemove()
*/
公共函数removeUpload()
{
如果($this->file==$this->getAbsolutePath()){
取消链接($this->file);
}
}
公共函数getAbsolutePath()
{
返回null==$this->path?null:$this->getUploadRootDir()./。$this->path;
}
公共函数getWebPath()
{
return null==$this->path?null:$this->getUploadDir()。
$this->id.'/.$this->path;
}
受保护的函数getUploadRootDir()
{
返回{uu DIR}/../../../../../web/'.$this->getUploadDir()./'.$this->id;
}
受保护的函数getUploadDir()
{
返回“上传/用户”;
}
/**
*设定路径
*
*@param string$path
*@返回用户
*/
公共函数setPath($path)
{
$this->path=$path;
退还$this;
}
/**
*获取路径
*
*@返回字符串
*/
公共函数getPath()
{
返回$this->path;
}
/**
*@param UploadedFile$file
*@返回对象
*/
公共函数setFile(UploadedFile$file=null)
{
$this->upload();
退还$this;
}
/**
*获取用于上载配置文件图片的文件
*
*@return-UploadedFile
*/
公共函数getFile()
{
返回$this->file;
}
/**
*@ORM\PrePersist()
*@ORM\PreUpdate()
*/
公共函数预上载()
{
如果(null!=$this->getFile()){
//上传了一个文件
//生成唯一的文件名
$filename=$this->generateRandomProfilePictureFilename();
$this->setPath($filename..。$this->getFile()->guessExtension());
}
}
/**
*生成一个32字符长的随机文件名
*
*@返回字符串
*/
公共函数生成器DomainProfilePictureFileName()
{
$count=0;
做{
$generator=新的SecureRandom();
$random=$generator->nextBytes(16);
$randomString=bin2hex($random);
$count++;
}而(文件_存在($this->getUploadRootDir()./'.$randomString'...$this->getFile()->guessExtension())和&$count<50);
返回$randomString;
}
/**
*@ORM\PostPersist()
*@ORM\postpdate()
*/
公共函数上传()
{
如果(null==$this->file){
返回;
}
$this->getFile()->move($this->getUploadRootDir(),$this->getPath());
如果(isset($this->tempPath)和&file_存在($this->getUploadRootDir()./')。$this->tempPath)){
取消链接($this->getUploadRootDir()./。$this->tempPath);
$this->tempPath=null;
}
$this->file=null;
}
}
数据库行预览:

有什么问题

namespace Session\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Util\SecureRandom;


/**
 * User
 *
 * @ORM\Table(name="User")
 * @ORM\Entity(repositoryClass="Session\UserBundle\Entity\UserRepository")
 */
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\PostRemove()
     */
    public function removeUpload()
    {
        if ($this->file == $this->getAbsolutePath()) {
            unlink($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->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;
    }


    /**
     * @param UploadedFile $file
     * @return object
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->upload();

        return $this;
    }

    /**
     * Get the file used for profile picture uploads
     *
     * @return UploadedFile
     */
    public function getFile()
    {

        return $this->file;
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // a file was uploaded
            // generate a unique filename
            $filename = $this->generateRandomProfilePictureFilename();
            $this->setPath($filename . '.' . $this->getFile()->guessExtension());
        }
    }

    /**
     * Generates a 32 char long random filename
     *
     * @return string
     */
    public function generateRandomProfilePictureFilename()
    {
        $count = 0;
        do {
            $generator = new SecureRandom();
            $random = $generator->nextBytes(16);
            $randomString = bin2hex($random);
            $count++;
        } while (file_exists($this->getUploadRootDir() . '/' . $randomString . '.' . $this->getFile()->guessExtension()) && $count < 50);

        return $randomString;
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }
        $this->getFile()->move($this->getUploadRootDir(), $this->getPath());

        if (isset($this->tempPath) && file_exists($this->getUploadRootDir() . '/' . $this->tempPath)) {
            unlink($this->getUploadRootDir() . '/' . $this->tempPath);
            $this->tempPath = null;
        }
        $this->file = null;
    }

}