symfonyflex-如何创建数据库mysql连接

3ks5zfa0  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(149)

我是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];
    }
}

我试着用我在网上找到的所有东西来创造它。不幸的是,我得到一个错误“调用一个成员函数get()on null”。
有my 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);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题