09.
SELECTing into a New DTO Object
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
18 Comments
Actually, in this specific case I would still probably prefer selecting to an array, because then I could just spread it in the controller:
But it's still a cool lesson, good to know this feature exists!
Hey Andrey,
Thanks for sharing this tip! Yeah, the new PHP syntax gives you more flexibility and in some cases might be pretty useful. DTO is also cool because it gives you an object and autocompletion in some cases, just choose wherever fits your needs better.
Cheers!
Woah!!!
This is the reason why I keep doing almost every SymfonyCasts tutorials. Even though I'm very familiar with Doctrine, I did not know that trick! It made my day!
Keep it up, friends!
You're welcome @julien_bonnier :)
Hello there!
I have a question on the SQL functions that we used in this tuto are available because we used in this tutorial beberlei/DoctrineExtensions. But currently, this project is "well blocked" to PHP 8.0 instead of PHP 8.2, and in November PHP 8.3.
Has anyone found a solution to this? I searched on packagist and github and found nothing maintained.
Should I switch to native SQL queries to be on the safe side for the future on long-term projects? But you lose the object ...
Hey @vince-amstz
This tutorial was built on PHP 8.1, and I assume it should work with 8.2 too. Anyways, I noticed that
DoctrineExtensionslibrary added support for PHP 8.2 https://github.com/beberlei/DoctrineExtensions/commit/7cfeb9ce1265f43d5007362d0ef9f7c9c68015ef.Did you get a problem trying to use it in your project?
Cheers!
Yes, you're right, it works. I thought PHP was in 8.0 on the tutorial and that's why it worked.
I had this question just before using it in another project, I saw the commit you sent but also that the last relase was 3 years old and supported PHP 8.0
https://github.com/beberlei/DoctrineExtensions/releases/tag/v1.3.0
Am I missing something?
Oh, yea, you're looking at version 1.3.0, they are still active but it seems like they have not released any new version yet, they have been committing everything to master https://github.com/beberlei/DoctrineExtensions/commits/master
I don't know the reason but perhaps you could open a ticket in the repository
hello,
I have a question regarding the use of aliases for the DTO, I'm trying to do this, except that for each entity I have the name field that I want to hydrate the DTO with this, but I can't do it!
AND when I try to do this, I get an error, if someone could help me please
Error message :
[Syntax Error] line 0, col 587: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_CLOSE_PARENTHESIS, got 'as'Hey @Sidi-LEKHAIFA ,
Hm, tricky case... you can try to remove that
askeyword, it should be option in SQL, so writing field aliases asp.name AS partenaireandp.name partenaireare both valid. Maybe it might work this way... but I think it will not work either because seems that does not support aliases at all. If so, the only possible solution is to name the field in the DB to make it unique. That will not be ideal, but still a valid workaround.But even with duplicated column names, I suppose it should still work? Did you try to use it without aliases? Is "partenaire" overridden by "medium" and then by "source"? IIRC that should pass fields to arguments by ordinal numbers.
Cheers!
Hello @Victor,
in fact, renaming fields doesn't seem very sensible to me, since I use these fields in a lot of places.
I thought it would overload the values since they'd have the same aliases, but no, it works perfectly, so I don't need the aliases.
Thank you very much
Hey @Sidi-LEKHAIFA
Perfect! Yeah, that's how it's supposed to work I think. And thanks for confirming it works without overrides :)
Cheers!
Hello,
Great this video :).
In my query I have both a select() and an addSelect(). How could I do to have this New DTO Object system?
Hey @seb-jean ,
I think you need to rewrite your logic into a single
select(), i.e. prepare everything you need before theselect()and use it there :)Cheers!
Thanks Victor
It's really great this way, do you know if it only works if the DTO hydrates through the constructor or if the DTO can hydrate through the getters and setters?
Hey @Sidi-LEKHAIFA
That's a good question. I believe Doctrine does not support setter methods. They are not explicit about it in the docs though https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#new-operator-syntax
I'd just give it a try to confirm it
Cheers!
Thanks @MolloKhan for your answer, yes I confirm that it doesn't work with setters, I just tried it. but it's good to know
"Houston: no signs of life"
Start the conversation!