I'm trying to use Doctrine DBAL in my Symfony web project.
What I've done:
Config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
parameters.yml
parameters:
database_host: 127.0.0.1
database_port: 5432
database_name: my_project_db
database_user: postgres
database_password: my_password
My controller for company profile
/**
* @Route("/profile/{userId}/{sessionKey}", name="profile")
*/
public function profileAction($userId=null, $sessionKey=null)
{
$securityController = new SecurityController();
$securityController->userId = $userId;
$securityController->sessionKey = $sessionKey;
if ($securityController->sessionKeyIsValid())
{
return new Response('Profile');
}
else
{
return $this->redirectToRoute('login');
}
}
And my securityController where I'm trying to connect to my Postresql
class SecurityController extends Controller
{
public $userId;
public $sessionKey;
public function sessionKeyIsValid()
{
$conn = $this->get('database_connection');
//$conn->fetchAll('select * from main.users;');
return false;
}
}
Exception (Error: Call to a member function get() on null
) appears on this string of code $conn = $this->get('database_connection');
What I'm doing wrong? It seems that I followed step by step as is it advised in Symfony cookBook http://symfony.com/doc/current/cookbook/doctrine/dbal.html