Symfony Flex-如何创建数据库mysql连接

时间:2018-10-26 11:43:27

标签: php mysql symfony

我是Symfony的初学者。 我正在尝试创建一个类来使用我的数据库(mysql)。但是我不能成功。你能帮助我吗 ? 这是我目前的课程:

<?php

namespace App\Database;

use Doctrine;
use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;


class DatabaseApi extends AbstractController
{
    /**
     * @var Connection
     */
    protected $database = null;
    protected static $instance = null;

    /**
     * DatabaseApi constructor.
     */
    protected function __construct()
    {
        $database = $this->container->get('database');
        $this->database = $database;
    }

    public static function getInstance()
    {
        self::$instance = new self();
        return self::$instance;
    }

    private function __clone()
    {
    }

    /**
     * @param $authentication_key
     * @return mixed
     * @throws Doctrine\DBAL\DBALException
     */
    public function select_authentication_key($authentication_key)
    {
        $auth_key_query = $this->database->prepare('CALL Verify_authentication_key(:authentication_key)');
        $auth_key_query->bindValue('authentication_key', $authentication_key);
        $auth_key_query->execute();

        $results = $auth_key_query->fetchAll(\PDO::FETCH_ASSOC);
        return $results[0];
    }
}

我试图用我在互联网上可以找到的所有东西来创建它。不幸的是,我收到一条错误消息“在null上调用成员函数get()”。


有我的services.yaml文件:

parameters:
    database_connection: default

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    database:
        alias: doctrine.dbal.%database_connection%_connection

这就是我尝试调用该函数的方法:

$database = DatabaseApi::getInstance();
$result = null;
try{
    $result = $database->select_authentication_key("1111-1111-1111-1111");
}catch (Exception $e){
    print($e);
}
print_r($result);

0 个答案:

没有答案