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.

Doctrine2: CHAR with annotations

Posted November 7th, 2012 in Razno by Metod

Ever wondered if there is a better way of defining a CHAR (mysql) with doctrine2 annotations?

From doctrine2 docs:

  1. <?php
  2. /**
  3.  * @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
  4.  */
  5. protected $country;

Not a very nice approach imo. I dug around the code and found a somewhat neater solution:

  1. <?php
  2. /**
  3.  * @Column(type="string", length=2, options={"fixed" = true})
  4.  */
  5. protected $country;

Now you don’t need to manually type in column definitions. 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. }

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:


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.

Login event listener in Symfony2

Posted July 11th, 2011 in Tehnikalije by Metod

So you have a Symfony2 project under construction. Login works. Now you want to execute some code right after the user successfully logs in.

The solution: Make a custom event listener.

An event listener is actually a service with a proper tag associated with it. So first step is to register the listener. The configuration file shown corresponds to the listener in file Acme/UserBundle/Listener/LoginListener.php. You can read more about services here.

In services.yml:

Let’s go through the configuration.

class defines the listener class.

With arguments we inject services that this listener depends on. In this case the security.context service for retrieving the User object and doctrine service for EntityManager.

tags is important. Every listener must be tagged with kernel.event_listener and must specify it’s event. For successful login that event is security.interactive_login. You can also specify the optional method tag.

Ok, our listener is registered. Let’s create it.

The above code should be pretty self-explanatory. The only required method is onSecurityInteractiveLogin(InteractiveLoginEvent $event). You do not need the other stuff, but you probably will if you want to modify the User object, or something else in the DB.