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.

Symfony Blog Bundle

HarentiusBlogBundle - bundle for a blog/simple portal (or complex, if using bundle inheritance and extension). View of this site.

As a backend, SonataAdminBundle is used. Implemented:

1. Admin panel (WYSIWYG, ckeditor), image/audio uploading, player.

2. Tags, categories, archives, tag cloud

3. Statistics

4. RSS-feed (requires modification)

5. "Smart" caching of everything, content is served very fast

To "ease the soul":

1. Tests

Symfony Widgets Bundle

Widgets Bundle - a bundle for easy widgets management. (Supports only widgets which require only client-side code for displaying). Includes both client side (for displaying) and admin side (adds admin classes and has a SonataAdminBundle dependency) functionality.

Can be used (for example) for adding counters, banners, advertising network codes (google adsense, etc).

Was created during this blog has been developing.

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: