Symfony Live (continued)

Peaks at Symfony Live 2019

After these two days Symfony Live 2019 in Paris, I now want to share my feelings with you and above all present to you what will be useful to us, developers, to work more efficiently.  

The conferences are aimed at developers. It’s technical, there’s code, it’s concrete! And this is very well so. We are presented with components, methods, particular cases. After these two days, we can only see that Symfony continues in its philosophy initiated with Flex, it is no longer just a framework but an ecosystem of open source components that work in symphony. Among the conferences that I found the most useful and interesting, I will present to you the one on Doctrine, and those on HTTP: HTTP3 and the new HTTP component.

In-depth doctrine

Every Symfony developer knows Doctrine the ORM by default, but it turns out that we don't know this component as well as it seems. We have been sold Doctrine as a magical system that is capable of doing everything on its own. For those who believed in the myth, this conference has shattered it. 

Indeed, Doctrine can be a performance disaster if entities are configured “by default”, the main problem with this: relationships between entities. 

Lazyloading, friend or foe? 

Poorly exploited, lazyloading can ironically kill application performance by multiplying requests. 

Among the solutions, we have: 

  • The most atypical: partial hydration where we only recover part of the object 
  • The most effective: DQL and using joins to retrieve associated entities, which is like doing SQL but objects 
  • The simplest: use FETCH_MODE=EAGER on an entity relationship. This will systematically retrieve the associated entity. If you need it every time it's perfect, otherwise you will unnecessarily burden the memory and the work of the database 

Little tip: as you know, unloaded associated entities are initialized with a proxy, to deserialize them, you must use the magic __load() method  

filters

They are little known but it is possible to add a filter to all queries by creating a class and extending the SQLFilter class, defined in the configuration. The filter can then be activated/deactivated via the entity manager if necessary. The most striking example: the use of a locale. With a filter, you no longer need to add this locale to all your queries! 

Exploit the DQL

We don't think about it enough but the QueryBuilder is an object and it allows us to divide our queries into methods. For example: a method to manage a condition, another for a select etc. Finally, we build our query by calling the different methods. Effective ! 

The flush and its tracking policies

It’s quite little known but Doctrine’s ‘flush’ has several strategies called “tracking policies”. By default Doctrine uses “implicit deferred”: it compares all fields of all entities (even non-persisted ones!), and generates the corresponding queries. Be careful: if you manipulate a lot of objects, this can consume a lot of memory. 

There is also the "deferred explicit" or only the persisted entities, the cascades {persist}are ignored) and "notify": each entity must signal its modifications to a listener. It's heavy but it's the most efficient. To manage this, one must use doctrine lifecycles.  

On the other hand, I am a little disappointed in the part concerning the cache: I would have liked more precision, on the serialization/deserialization of entities and their associations to store them in systems such as Redis, Memcached, etc.  

Symfony HTTP Client, let’s move up a gear

The new HTTP component aims to replace HTTP Foundation and Guzzle, and know that it has the means to make you want to change. 

Already, the syntax will please a lot of people, but so will these serialization/deserialization methods! It natively supports some authentication methods (e.g. bearer) simply via a parameter. 

Asynchronous with PHP

But by far the most impressive feature is the stream method. It is possible to process the response(s) as they arrive, but also to process the content of the response as you go! With this system, we forget one of the biggest weaknesses of PHP: asynchronous management! On large or multi-responses (especially if the resource is a file), the time saving is considerable. 

Classy and beautiful decoration 

Among all the new classes, HttpBrowser replaces the crawler, NativeHttpClient is the lowest level class, useful if you need to manage your requests more carefully and in more detail. Decorators are available, notably for generating mocks and managing the cache. All its components replace Goutte which will no longer be maintained. This is normal since it was Fabien Potencier who maintained it until then. 

Http3

Quickly, I move on to the HTTP 3 conference which is already very well documented. 

Abandonment of the aging TCP protocol for a new one called QUIC (Quick UDP Internet Connections) which is based on UDP. On the menu and in general: end of coupling with the network connection, native integration of encryption (and therefore only one request per connection), better package handling, and browser-based package handling. 

The conference had the merit of being realistic about its implementation, which will change a lot of things, and it is not going to be easy or quick. 

Nevertheless, this new version is promising. We just have to hope that the multitude of players involved play the game. Including the ISPs, who will have to manage UDP differently on their boxes. 

You will surely have understood this from reading me but I found this show very interesting. Developers talking to other developers works, and I recommend you get involved. The Symfony ecosystem is rich and evolving, and it’s nice to see that so many enthusiasts are contributing to it!  

Marine

Check all
Career area
Subscribe to the newsletter :
These articles may interest you