Data Transfer Object is an element used to pass data between various places in application. It’s only role comes down to store data. It does not contain any logic.
Let’s assume we need data from database to create some report. Thera are a planty ways to do it. We can retrieve entities and then get interesting data from those entities. However, it will not probably be the most optimum way. We can also get raw data form database. But then there will be no type checking and so on. In Doctrine there is a simpler way.
Doctrine from version 2.4 allows to fetch data as DTO in a very pleasent way. It creates objects for us, setting the correct types the class has in the constructor. The only limitation is that the objects can be created from scalar types.
Let’s assume there are two entities: User and Income.
Database contains such data:
As shown above, there are two users with some income in each month.
The report requires whole user income so the DTO class may look like this:
To get aggregated data we need to use a Query Builder to create a query:
The creation of DTO object s occurs by adding a NEW operator with the fully qualified class name with the particular constructor arguments.
The results are as follows:
The way described above greately facilitates the creation of data transfer objects. Instead of fetching whole entities and extracting data from them, we can immedietly retrieve objects filled with data. Thanks to this we can save a lot of processor cycles, as well as avoid writing many more lines of code