douxie3625
2016-06-25 17:06
浏览 59
已采纳

使用Doctrine DBAL的Php Symfony 3.1异常

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题