Detect if a synthetic service has been set in Symfony2 Dependency Injection container

Posted October 17th, 2014 in Symfony2 by Metod

If you are using the Symfony2 DependencyInjection component, you might have run into synthetic services.

If I quote from the docs:

Synthetic services are services that are injected into the container instead of being created by the container.

So, what do we do if we want to detect if a service has already been injected or not? Turns out that the definition of the synthetic service gets removed when it is injected into the container. So we can just check if the definition for the service exists. Below is a quick gist to get the general idea.

Get Doctrine2 DBAL connection for different databases in Symfony2

Posted October 26th, 2013 in Razno by Metod

Let’s say you have multiple databases you want to connect to and your config looks similar to the following:

So we have a default DBAL connection to db1 and we have 2 other databases db2 and db3 we want to connect to and perform some queries on.

How do we get the DBAL connections to them?

The Cookbook only tells us how to get the default (db1) connection:

$conDb1 = $this->get('database_connection');

If we want the connection to other 2, it goes like this:

$conDb2 = $this->get('doctrine.dbal.db2_connection');
$conDb3 = $this->get('doctrine.dbal.db3_connection');

The key part is of course: doctrine.dbal.%s_connection, where %s is replaced by the connection name you want to get.

Fix for: There is no `_sonata_admin` defined for the controller …

Posted December 25th, 2012 in Razno by Metod

If you stumbled upon the following error while trying to make SonataAdminBundle work:

There is no `_sonata_admin` defined for the controller `Acme\DemoBundle\Controller\PostController` and the current route `admin_acme_demo_post_list`

Here is what is wrong with your configuration.

The official documentation (in my case for ORM) says to define the *Admin service as follows:

# app/config/config.yml
      class: Acme\DemoBundle\Admin\PostAdmin
        - { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
      arguments: [null, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Notice that null in the arguments? That is what is wrong.

That means that as the code for the admin it passes null. Whereas the correct thing to do would be to pass in the name of the service itself. Like this:

# app/config/config.yml
      class: Acme\DemoBundle\Admin\PostAdmin
        - { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
      arguments: [, Acme\DemoBundle\Entity\Post, AcmeDemoBundle:PostAdmin]

Happy coding!

Symfony2: Upgrading 2.0 to 2.1 – CallbackValidator

Posted November 12th, 2012 in Razno by Metod

The CallbackValidator class was deprecated in 2.1.

If you implemented custom validators using this interface, you can substitute them by event listeners listening to the FormEvents::POST_BIND (or any other of the *BIND events). In case you used the CallbackValidator class, you should now pass the callback directly to addEventListener.

Here is how you do it.



Enjoy! 😉

Symfony2: Bundle does not contain any mapped entities

Posted September 14th, 2012 in Symfony2 by Metod

There is one more tip to solving this mistery. Make sure you have a namespace statement in the entity line.

This will cause the error. But if you add the namespace (duh moment) it will work again.

Symfony2: How to set the correct host for routing in console command

Posted March 31st, 2012 in Symfony2, Tehnikalije by Metod

Quick tip for setting the correct host in your custom symfony2 command.


This will set your host correctly for routing and it will no longer default to ‘localhost’.

A bit longer version:

Serialization of UploadedFile is not allowed fix

Posted January 31st, 2012 in Razno, Tehnikalije by Metod

If you are doing file uploads in Symfony2 with help from the cookbook tutorial you might have ran into the following error:

Serialization of ‘Symfony\Component\HttpFoundation\File\UploadedFile’ is not allowed.

The solution I came up with involves excluding the public $file field from serialization. That way we bypass the exception.

One catch, though: this will exclude all fields that are not protected.

Add the following function to your entity:


Check if symfony2 form has errors in a twig template

Posted January 16th, 2012 in Razno by Metod

Edit: The above way became deprecated in 2.1 and removed in 2.3. The new way:

Mysql CHAR_LENGTH function for Doctrine2

Posted January 15th, 2012 in Projects by Metod

Today I needed the CHAR_LENGTH function for use in DQL in Doctrine2 project. So I wrote one myself and sent a pull request to DoctrineExtensions so as to contribute to OS community.

You can get the file on github:


And a quick tip on how to activate it in Symfony2:

How to use Pagerfanta with Symfony2 and Doctrine2

Posted November 27th, 2011 in Tehnikalije by Metod

So let’s assume you want to use a pager with your Symfony2 application. If you are like me, you type it in google and get a link to KnpPaginatorBundle. Start using it and everything goes well, until you hit an error telling you something is wrong. That something is something in the lines of:

SELECT SUM(S.STH) AS something FROM ...

Which Zend_Paginator does not support! Here is where Pagerfanta comes into the picture.

Download the PagerfantaBundle or get it through git. Also get Pagerfanta. I decided to break PagerfantaBundle into 2 more folders. So the structure of the bundle is now:


For Pagerfanta, the structure is:


Now, add both to autoload:

// app/autoload.php
    // ...
    'WhiteOctober'     => __DIR__.'/../vendor/bundles',
    'Pagerfanta'       => __DIR__.'/../vendor/pagerfanta/src',
    // ...

Register the bundle with your application kernel:

// app/AppKernel.php
public function registerBundles()
    return array(
        // ...
        new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
        // ...

Now what the official documentation does not tell you is what namespaces to include to use it.

For Doctrine2 the example Controller looks like this:

In the view (I assume you are using Twig) you just do:

{{ pagerfanta(examples) }}

And you can be done!

However, there is much more to Pagerfanta. You can pass in many configuration options. Here is where so called Global Variables come in handy. You can define them in app/config/config.yml:

            prev_message: '«'
            next_message: '»'
            dots_message: ' ... '

Now you can access the ‘pagerfanta_opts’ in every template:

{{ pagerfanta(examples, 'default', pagerfanta_opts) }}

Where ‘default’ is the name of the Pagerfanta view.

For those who want to learn more about it, read the documentation on GitHub for Pagerfanta.