Refreshing Nested Set Tree Indexes in Symfony

Tree

Once I used Nestedset behavior for Doctrine, Symfony in a project, and faced issue with broken indexes. (You can read how Nested Set works here). This happened because low-level MySQL query executed during migration, and because of this Doctrine events responsible for refreshing indexes was not executed. To fix that a Symfony Command was written, which refreshes indexes. This is just example. Better way is to move this code to the service and add one more migration which will refresh indexes. Here is just simple example how this issue can be solved.

Argument of the command is an entity to refresh.

Uploading files in Symfony

Uploading files in Symfony is conceptually no different from other PHP platforms, but still has its own features due to the presence of additional tools provided by the framework.

First of all, it is worth noting that there are ready-made solutions that solve the task at hand. I strongly recommend familiarizing yourself with them, and only after that, if you decide that they do not suit you, implement your own solution.

In this note we will try to show possible ways to solve the task, using both ready-made solutions (VichUploaderBundle, IphpFileStoreBundle), and using our own implementation (in Symfony controllers and admin classes from the SonataAdminBundle).

Remove the default block sonata.admin.block.admin_list in SonataAdminBundle.

When using SonataAdminBundle and SonataBlockBundle, I wanted to remove the default block.

It doesn't provide much benefit, as it simply duplicates the functionality of the side menu on the dashboard.

When installing SonataBlockBundle, if we simply write in the configuration:

sonata_block:
    default_contexts: [sonata_page_bundle]
    blocks: []

(in the future we will add our own blocks, but for now we just leave an empty array), we will catch an error.

Caching Symfony controller

During developing this blog I invented one more bicycle for Caching Symfony controller. But first of all lets see how did this task arose.

For example, I have a list of categories, archives and tags on a sidebar. It is relatively easy to get last one (one query), but much harder to get list of categories and archives. For getting categories, we need to select trees (categories can be nested) and using subqueries inside queries get number of articles in every category (result is a little bit monster). For getting the archives list, we need iterate over all articles and gather list of years/months. All this actions isn't very sophisticated, but it is better to avoid them.

Localization of numeric/monetary data

When developing a medium to large project, there is a problem with localizing numeric/money data. In this note, I will talk about the difficulties of using the Symfony framework, Sonata Admin Bundle, and the client-side. But first, let's discuss the essence of the problem, as it is not very obvious at first glance.

So, let's say we have a project that renders numeric/money data on the server side (php/template engine). They should be displayed according to the set locale. The user can enter data (in their own representation). At the same time, the data can be processed on the client side (javascript). For example, in most European countries, except for the UK and Ireland, the decimal separator is a comma while in the UK and Ireland it is a period. Naturally, a user from Germany will enter data with a comma separator.

Customizing login form (using FOSUserBundle as an example)

The simplest way to customize the login form in FOSUserBundle is to use the mechanism of bundle inheritance. (Assuming that in the future it will be necessary to modify not only the login form). Let's show an example of such customization using bootstrap.

Fixtures. AliceBundle

In the previous article, we learned about fixtures in the Symfony framework, as well as the DoctrineFixturesBundle. Let's take a look at another useful bundle for working with fixtures - AliceBundle (a wrapper around the alice component).

Fixtures. DoctrineFixturesBundle

Fixtures (Eng. fixtures) - a very useful development tool. Essentially, it is just a set of test data used in dev mode. They are typically not used in prod mode (for production, Data Migrations are usually used).

There are several convenient bundles for working with fixtures in Symfony. The first one is the basic DoctrineFixturesBundle, which is dedicated to this article.

Configuration

When creating a bundle, it is useful to configure it, which will be used as a library (and not only).

Of course, you can put everything in parameters, thus avoiding configuration, but that would not be kosher. =)

Proper practice is to describe the configuration in config.yml, and to extract some necessary parameters into parameters.yml:

Inheritance of bundles

In the Symfony framework, there is an interesting system of bundle inheritance. It is interesting because it works like object-oriented inheritance, but in reverse. When a bundle is inherited, modifications also affect the parent bundle. This is very convenient: if you don't like a certain component or you want to extend/replace its implementation, you can use the mechanism of bundle inheritance without modifying the parent bundle. However, when using the parent bundle, the new functionality will be used.

Service Container and Dependency Injection in Symfony framework

Sometimes a single idea can change everything about programming. For me, one of those ideas was the concept of a service container. It's like the holy grail). Actually, the idea itself is not specific to Symfony framework, it's just one of the successful patterns of application design often used in Symfony.

Collection Bundle

Aside from the standard Symfony distribution, there are many bundles from third-party developers that greatly simplify application development (after all, why reinvent the wheel when there is a ready-made solution available). Let's not get into a debate about the often presence of functionality in bundles that will never be used in your application—development speed will cover these fairly minor issues. Here is a modest collection of bundles that greatly simplify life.

To install a bundle, first add it to your project using composer, for example:

composer require "presta/sitemap-bundle:~1.4"


Doctrine. Structure. Entity

Right after starting to work with Doctrine I acquired some prejudices. I have since eradicated them, but maybe someone still has them, maybe this note will help someone =)

Using assetic in the Symfony framework

A few notes back I considered working with assets in the Symfony framework. This example shows how to work with your own assets. But in practice, it is often necessary to use external assets.

For such, and not only such tasks, assetic is suitable. It is quite difficult to describe in a few words what it is. But I imagine assetic as an asset manager for myself.