Chapters
40 Chapters
|
4:39:58
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
29.
Edit Endpoint & Deserialization
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4 but works great in Symfony 5!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.87.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.9.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.0.3
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.2.3
"symfony/console": "^4.0", // v4.2.3
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.2.3
"symfony/framework-bundle": "^4.0", // v4.2.3
"symfony/property-access": "4.2.*", // v4.2.3
"symfony/property-info": "4.2.*", // v4.2.3
"symfony/security-bundle": "^4.0", // v4.2.3
"symfony/serializer": "4.2.*", // v4.2.3
"symfony/twig-bundle": "^4.0", // v4.2.3
"symfony/validator": "^4.0", // v4.2.3
"symfony/web-server-bundle": "^4.0", // v4.2.3
"symfony/yaml": "^4.0", // v4.2.3
"twig/extensions": "^1.5" // v1.5.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
"symfony/dotenv": "^4.0", // v4.2.3
"symfony/maker-bundle": "^1.0", // v1.11.3
"symfony/monolog-bundle": "^3.0", // v3.3.1
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
"symfony/stopwatch": "4.2.*", // v4.2.3
"symfony/var-dumper": "^3.3|^4.0", // v4.2.3
"symfony/web-profiler-bundle": "4.2.*" // v4.2.3
}
}
7 Comments
Hi Ryan, thank you for the video. This block is not updating the passed reference obj
If I dump $reference right after, I get the same old values and the file name is not updated. Any thoughts?
P.S. I even copy/pasted your code to make sure I hadn't mistyped something, but it's the same thing.
Your help would be very much appreciated.
Thanks
Hi Diana E.!
Hmmm. I'm not sure what's going on here, so let's try a few things:
A) Try setting the return value of
deserialize()to a value and dump it. Also dump$reference:The question: are these objects identical or are these 2 different objects?
B) If they ARE identical, then my guess is that the
object_to_populatesystem IS working... but for some reason, the filename is not being updated. To test this theory, remove theobject_to_populateoption anddump($deserializedReference);. Is the filename set in this new object or not?Let me know what you find out!
Cheers!
Thanks for your response Ryan. In A, I do get identical objects:
ArticleReference {#786 ▼
-id: 1
-article: Article {#833 ▶}
-filename: "mg-6694_6124e8eb927da.jpeg"
-originalFilename: "_MG_6694.jpg"
-mimeType: "image/jpeg"
}
Hey Diana E. !
Hmm. Ok! This tells me that the
object_to_populatesystem IS working: the serializer IS trying to update your existing object.Ok, so to review, the goal is to be able to send an
originalFilenameJSON property and have it update theoriginalFilenameproperty on your entity. There are a few things to check out (you may have checked some of these already, but just in case):1) Make sure you have a setOriginalFilename() method
2) Make sure that the
originalFilenameproperty is in theinputserialization group3)
dump($request->getContent())on your controller to be absolutely sure that this contains aoriginalFilenamefield.4) Add a temporary
dump($originalFilename)inside of yoursetOriginalFilename()method to make sure that this code is being executed.As a reminder, to see of those
dump()lines are being hit, after triggering the AJAX call, you can use this trick to see the profiler (and then the "dump" tab on the profiler) to see what those output: https://symfonycasts.com/screencast/symfony-uploads/dropzoneLet me know what you find out!
Cheers!
Thanks a lot for your help, Ryan. The issue was no. 2 (serialisation group).
i dunno if you guys check comments for this article yet but i have a question what if we use react instead of twig ? how can our api send files to our front so we can update them and send them back ? any suggestions would be appreciated !
Hey @Hamza!
> how can our api send files to our front so we can update them and send them back
Hmm. So this sounds like 2 parts.
First "how can can the API return files that we've already uploaded". The answer to this is no different if you're using React or something else - it's more of a general "API question". I think there are generally 2 approaches:
1) You have an endpoint that returns information about the file (e.g. filename) including one field that is the base64 encoded version of the binary string. This works great for smaller files (base64 encoding makes file sizes a bit bigger).
2) You have an endpoint that returns information about the file (e.g. filename) including one field that is the URL to where the resource could be downloaded, like a URL on your site (e.g. even /uploads/file/foo.jpg) or on a CDN, like cloudfront.
Second "how can we send a new file back to the server"?
We talk about that here: https://symfonycasts.com/sc... - and it shouldn't be any different if you're using React.
Let me know if that helps :).
Cheers!
"Houston: no signs of life"
Start the conversation!