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
+67
Original file line number
Diff line number
Diff line change
@@ -490,3 +490,70 @@ 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 may have to take care of.
497
+
498
+
> [!NOTE]
499
+
> If you only expose file uploads through API Platform’s endpoints (e.g. POST /media_objects or POST /users/{id}/avatar), you don’t need any special serialization hooks and you can ignore this part.
500
+
> This part is only relevant if you try to attach an `UploadedFile` or `File` object directly to your `User` entity in a non-API context (for example via a Symfony form controller where the `User` is stored in the session).
501
+
502
+
Symfony’s session handler will attempt to serialize the entire `User` object when it stores your `User` in the session (for example via the Security token in a Symfony form controller).
503
+
Since `File` is not serializable by default, you’ll encounter:
504
+
```
505
+
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
506
+
```
507
+
508
+
To fix this, you need to implement both the old `\Serializable` interface **and**, starting with PHP 7.4+, the new `__serialize()` / `__unserialize()` magic methods on your `User`, limiting serialization to just scalar fields (`id`, `email`, `password`, etc.).
509
+
Symfony will then happily keep your `User` in the session while VichUploaderBundle handles the file itself.
510
+
511
+
```php
512
+
#[Vich\Uploadable]
513
+
#[ORM\Entity]
514
+
#[ApiResource(...)]
515
+
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable
516
+
{
517
+
private ?Uuid $id;
518
+
private ?string $email;
519
+
private ?string $password;
520
+
521
+
// …
522
+
523
+
// Legacy Serializable, still used by Symfony SessionStorage
0 commit comments