Fix SoftDeletable Doctrine Entities with Unique Index Columns (Symfony2)

If you are using the Gedmo SoftDeletable Doctrine extension for entities that have some unique index columns, you will probably encounter a MySQL error when trying to add new entity with the same values as one previously deleted:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ... for key …

Find out how to avoid this by reading this post: Symfony2: Gedmo SoftDeletable Doctrine Entities with Unique Index Columns.

How to get the current route and URL in Symfony2

To get the current URL in a Symfony2 project, use the following code.

In a controller:

public function someAction(\Symfony\Component\HttpFoundation\Request $request)
{
    $route = $request->get('_route');
    $url = $request->getUri();
}

In a TWIG template:


{{ app.request.get('_route') }}

{{ app.request.getUri }}

Why would you need this? For example to setup social share buttons for the current page.

Symfony2 – Getting translated validation errors in controller

I recently ran into a problem when creating an API call for a project. I needed to validate an entity and, in case of errors, return them to the requester. But having i18n (internationalization and localization) enabled for my project forced me to write the MinLength constraint message as a pluralized message: Password too short, enter at least one character|Password too short, enter at least {{ limit }} characters. Continue reading Symfony2 – Getting translated validation errors in controller