2016-07-05 1 views
1

StofDoctrineExtensionsBundle Uploadableを使用して、ユーザーエンティティの画像をアップロードします。 formTypeでStofDoctrineExtensionsBundleアップロード可能

... 

$em = $this->getDoctrine()->getManager(); 
$em->persist($user); 

$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager'); 
$uploadableManager->markEntityToUpload($user, $user->getPath()); 

... 

:コントローラで

<?php 

namespace Application\UserBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Gedmo\Mapping\Annotation as Gedmo; 

/** 
* @ORM\Entity 
* @ORM\Table(name="user") 
* @Gedmo\Uploadable(pathMethod="getPath", filenameGenerator="SHA1", allowOverwrite=true, maxSize="100000", allowedTypes="image/jpeg,image/pjpeg,image/png,image/x-png") 
*/ 
class User 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    ... 

    /** 
    * @ORM\Column(name="picture", type="string", length=255, nullable=true) 
    * @Gedmo\UploadableFilePath 
    */ 
    private $picture; 

    public function getPath() 
    { 
     return '/user'; 
    } 

    public function setPhoto($photo) 
    { 
     $this->photo = $photo; 

     return $this; 
    } 

    public function getPhoto() 
    { 
     return $this->photo; 
    } 


    ... 

... 

->add('picture', FileType::class, array(
    'label' => 'Picture', 
    'required' => false 
)) 

... 

config.yml:

:私はそれをテストする場合

# StofDoctrineExtensionsBundle Configuration 
stof_doctrine_extensions: 
    default_locale: fr_FR 
    uploadable: 
     # Default file path: This is one of the three ways you can configure the path for the Uploadable extension 
     default_file_path:  %kernel.root_dir%/../web/uploads 
     # Mime type guesser class: Optional. By default, we provide an adapter for the one present in the HttpFoundation component of Symfony 
     mime_type_guesser_class: Stof\DoctrineExtensionsBundle\Uploadable\MimeTypeGuesserAdapter 

     # Default file info class implementing FileInfoInterface: Optional. By default we provide a class which is prepared to receive an UploadedFile instance. 
     default_file_info_class: Stof\DoctrineExtensionsBundle\Uploadable\UploadedFileInfo 
    orm: 
     default: 
      uploadable: true 

Iは、メッセージを取得します

"/ user"ディレクトリを作成できません。

この問題を解決するための考え方。ありがとう

答えて

1

あなたのアプリはサーバーにありますか?その場合は、chmodを確認してください。

または(フォルダ構造がweb/userある場合)の先頭に/を削除:

public function getPath() 
{ 
    return '/user'; 
} 
+0

いいえ、私はまだ地元で働いています。 –

+0

ありがとうございます。最初に/を削除しました –

関連する問題