0
問題は、他のフィールドを変更した場合にのみアップロードされるという問題です。私はちょうどファイルをアップロードすることを選択した場合、それは動作しません。使用VichUploaderBundleファイルをaupladするのに役立ちます -Symfony FosUserBundle profilePicture
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $nom;
/**
* @ORM\Column(type="string", length=255)
*/
protected $prenom;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
* maxSizeMessage = "The maximum allowed file size is 5MB.",
* mimeTypesMessage = "Only the file types image are allowed.")
*/
public $file;
/**
* @Assert\File(maxSize="2048k")
* @Assert\Image(mimeTypesMessage="Please upload a valid image.")
*/
protected $profilePictureFile;
// for temporary storage
private $tempProfilePicturePath;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $profilePicturePath;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Set nom
*
* @param string $nom
*
* @return User
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* @param string $prenom
*
* @return User
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* @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 ($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';
}
/**
* @param string $path
* @return User
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* @return string
*/
public function getPath() {
return $this->path;
}
/**
* Set profilePicturePath
*
* @param string $profilePicturePath
*
* @return User
*/
public function setProfilePicturePath($profilePicturePath)
{
$this->profilePicturePath = $profilePicturePath;
return $this;
}
/**
* Get profilePicturePath
*
* @return string
*/
public function getProfilePicturePath()
{
return $this->profilePicturePath;
}
私のオーバーライドにformType ProfileType.phpが
class ProfileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom')->add('prenom')->add('file');
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\ProfileFormType';
}
public function getBlockPrefix()
{
return 'fos_user_profile_edit';
}
public function getName()
{
return $this->getBlockPrefix();
}
}
すべてのヘルプ