Accoring towards the Symfony 2 Documentation, make use of the next PHP code for connecting towards the database and perform query...

$conn = $this->get('database_connection');
$users = $conn->fetchAll('SELECT * FROM users');

I am a complete novice with Symfony 2 however i am familiar with OOP. I'm wondering whether it was possible to possess a globally available $conn variable will be able to access from the bundle. The $conn variable would contain the need for $this->get('database_connection') and so i do not have to retype $conn = $this->get('database_connection');it each time I wish to create a new query.


global variables are more often than not NOT something you would like in OOP. They're confusing if this involves a technique which handles multiple variables plus they could even be hidden by local variables. For me personally, dealing with claims like

$anything = $this->get('what.the.hell.why.arent.those.identifiers.shorter');

is really as annoying for you and so i wound up in creating one subclass of SymfonyBundleFrameworkBundleControllerController per project which supplies techniques which call get using the actual identifiers. Inside your situation I'd produce a method

public function getDatabaseConnection()
    return $this->get('database_connection');

Generally - why not use Doctrine for controlling the DB connection? The majority of the queries can be achieved through the ORM and this is one way to utilize a genuine object-oriented interface towards the database. Consider it, I am also having fun with Symfony2/Doctrine since some days also it really seamless comfort. At first, it could seem like a hell of configuration, but when you have done the fundamental configs, the expansion is actually fast! :)