Last active
February 6, 2017 05:59
-
-
Save acucchieri/2c8ddc0da1473e16865f2c9c9c043361 to your computer and use it in GitHub Desktop.
Symfony Doctrine Uplodable with relation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/config/config.yml | |
| stof_doctrine_extensions: | |
| default_locale: "%locale%" | |
| 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/upload" | |
| # 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 # uploadable: ~ seems not work |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace AppBundle\Entity; | |
| use Doctrine\ORM\Mapping as ORM; | |
| use Gedmo\Mapping\Annotation as Gedmo; | |
| /** | |
| * Document | |
| * | |
| * @ORM\Table(name="document") | |
| * @ORM\Entity() | |
| * | |
| * @Gedmo\Uploadable( | |
| * filenameGenerator="SHA1", | |
| * allowOverwrite=true, | |
| * appendNumber=true | |
| * ) | |
| */ | |
| class Document | |
| { | |
| /** | |
| * @var int | |
| * | |
| * @ORM\Column(name="id", type="integer") | |
| * @ORM\Id | |
| * @ORM\GeneratedValue(strategy="AUTO") | |
| */ | |
| private $id; | |
| /** | |
| * FQFN ( /path/to/file.ext ) | |
| * | |
| * @var string | |
| * | |
| * @ORM\Column(name="path", type="string", length=255) | |
| * @Gedmo\UploadableFilePath | |
| */ | |
| private $path; | |
| /** | |
| * Name + Extension ( file.ext ) | |
| * | |
| * @var string | |
| * | |
| * @ORM\Column(name="name", type="string", length=255) | |
| * @Gedmo\UploadableFileName | |
| */ | |
| private $name; | |
| /** | |
| * Mime Type | |
| * | |
| * @var string | |
| * | |
| * @ORM\Column(name="mime", type="string", length=255) | |
| * @Gedmo\UploadableFileMimeType | |
| */ | |
| private $mime; | |
| /** | |
| * Size in bytes | |
| * | |
| * @var string | |
| * | |
| * @ORM\Column(name="size", type="decimal", precision=10, scale=2) | |
| * @Gedmo\UploadableFileSize | |
| */ | |
| private $size; | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace AppBundle\Form; | |
| // ... | |
| use Symfony\Component\Form\CallbackTransformer; | |
| use Symfony\Component\HttpFoundation\File\File; | |
| class DocumentType extends AbstractType | |
| { | |
| /** | |
| * @param FormBuilderInterface $builder | |
| * @param array $options | |
| */ | |
| public function buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| // ... | |
| ->add('path', FileType::class) | |
| ->get('path')->addModelTransformer(new CallbackTransformer( | |
| function ($string) { | |
| if (!$string){ | |
| return; | |
| } | |
| return new File($string); | |
| }, | |
| function (File $file = null) { | |
| return $file; | |
| } | |
| )) | |
| ; | |
| ; | |
| } | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace AppBundle\Entity; | |
| use Doctrine\ORM\Mapping as ORM; | |
| /** | |
| * User | |
| * | |
| * @ORM\Table(name="fos_user") | |
| * @ORM\Entity() | |
| */ | |
| class User | |
| { | |
| // ... | |
| /** | |
| * @ORM\OneToOne(targetEntity="Document", cascade={"persist", "remove"}) | |
| * @ORM\JoinColumn(name="document_id", referencedColumnName="id", nullable=true) | |
| */ | |
| private $avatar; | |
| // ... | |
| /** | |
| * @param \AppBundle\Entity\Document $avatar | |
| * | |
| * @return User | |
| */ | |
| public function setAvatar(\AppBundle\Entity\Document $avatar = null) | |
| { | |
| $this->avatar = $avatar; | |
| return $this; | |
| } | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace AppBundle\Controller; | |
| // ... | |
| class UserController extends Controller | |
| { | |
| public function newAction(Request $request) | |
| { | |
| // ... | |
| $em = $this->getDoctrine()->getManager(); | |
| $em->persist($user); | |
| $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager'); | |
| // Here, "getAvatar" returns the "UploadedFile" instance that the form bound in your $avatar property | |
| $uploadableManager->markEntityToUpload( | |
| $user->getAvatar(), | |
| $user->getAvatar()->getPath() | |
| ); | |
| $em->flush(); | |
| // ... | |
| } | |
| // same for editAction() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace AppBundle\Form; | |
| // ... | |
| use AppBundle\Form\DocumentType; | |
| class UserType extends AbstractType | |
| { | |
| /** | |
| * @param FormBuilderInterface $builder | |
| * @param array $options | |
| */ | |
| public function buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| // ... | |
| ->add('avatar', DocumentType::class) | |
| ; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. I have this same setup, but I'm getting a type mismatch error "Expected value of type "AuditBundle\Entity\File" for association field "AuditBundle\Entity\AuditEvidence#$file", got "Symfony\Component\HttpFoundation\File\UploadedFile" instead." Somewhere there doesn't seem to be a transformer being called??