You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: symfony/file-upload.md
+63
Original file line number
Diff line number
Diff line change
@@ -490,3 +490,66 @@ final class UploadedFileDenormalizer implements DenormalizerInterface
490
490
If you're not using `autowiring` and `autoconfiguring`, don't forget to register the service and tag it as `serializer.normalizer`.
491
491
492
492
For resolving the file URL, you can use a custom normalizer, like shown in [the previous example](#resolving-the-file-url).
493
+
494
+
## Uploading Files on Your User Entity
495
+
496
+
If you’d like to add file-upload support directly to your `User` entity (rather than a dedicated `MediaObject`), there’s one extra “gotcha” you must handle:
497
+
Symfony’s session storage will try to serialize your entire `User` object (including any `File` instance you’ve attached), and `File` is not serializable by default.
498
+
499
+
To avoid PHP errors like:
500
+
```
501
+
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
502
+
```
503
+
504
+
you need to implement both the old `\Serializable` interface **and**, depending on the PHP version, the new `__serialize()` / `__unserialize()` magic methods on your `User`, limiting serialization to just scalar fields (`id`, `email`, `password`, etc.).
505
+
Symfony will then happily keep your `User` in the session while VichUploaderBundle handles the file itself.
506
+
507
+
```php
508
+
#[Vich\Uploadable]
509
+
#[ORM\Entity]
510
+
#[ApiResource(...)]
511
+
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable
512
+
{
513
+
private ?Uuid $id;
514
+
private ?string $email;
515
+
private ?string $password;
516
+
517
+
// …
518
+
519
+
// Legacy Serializable, still used by Symfony SessionStorage
0 commit comments