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:

  1. $conDb1 = $this->get(‘database_connection’);

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

  1. $conDb2 = $this->get(‘doctrine.dbal.db2_connection’);
  2. $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:

  1. # app/config/config.yml
  2. services:
  4.       class: Acme\DemoBundle\Admin\PostAdmin
  5.       tags:
  6.         – { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
  7.       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:

  1. # app/config/config.yml
  2. services:
  4.       class: Acme\DemoBundle\Admin\PostAdmin
  5.       tags:
  6.         – { name: sonata.admin, manager_type: orm, group: acme_demo, label: post }
  7.       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.

  2. <?php
  4. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity
  8.  */
  9. class User
  10. {
  11.     // code here
  12. }

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

  1. <?php
  3. namespace Acme\DemoBundle\Entity;
  5. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity
  9.  */
  10. class User
  11. {
  12.     // code here
  13. }

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.

  1. $this->getContainer()->get(‘router’)->getContext()->setHost(‘’);

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:

  1. 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:

  1. vendor\bundles\WhiteOctober\PagerfantaBundle\…

For Pagerfanta, the structure is:

  1. vendor\pagerfanta\src\…

Now, add both to autoload:

  1. // app/autoload.php
  2. $loader->registerNamespaces(array(
  3.     // …
  4.     ‘WhiteOctober’     => __DIR__.‘/../vendor/bundles’,
  5.     ‘Pagerfanta’       => __DIR__.‘/../vendor/pagerfanta/src’,
  6.     // …
  7. ));

Register the bundle with your application kernel:

  1. // app/AppKernel.php
  2. public function registerBundles()
  3. {
  4.     return array(
  5.         // …
  6.         new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
  7.         // …
  8.     );
  9. }

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:

  1. {{ 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:

  1. twig:
  2.     globals:
  3.         pagerfanta_opts:
  4.             previous_message: ‘&laquo;’
  5.             next_message:     ‘&raquo;’
  6.             dots_message:     ‘ … ‘

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

  1. {{ 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.