0

I am a beginner on Symfony. I am trying to create a class to use my database (mysql). But I can not succeed. Can you help me ? Here is my current class:

<?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];
    }
}

I tried to create it with everything I could find on the Internet. Unfortunately, I get an error "Call to a member function get() on null".


There is my services.yaml file :

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

And is is how I try to call the fonction :

$database = DatabaseApi::getInstance();
$result = null;
try{
    $result = $database->select_authentication_key("1111-1111-1111-1111");
}catch (Exception $e){
    print($e);
}
print_r($result);
11
  • Did you configuer the database? symfony.com/doc/current/doctrine.html#configuring-the-database Commented Oct 26, 2018 at 11:47
  • If you don use symfony 4 but 3: symfony.com/doc/3.4/doctrine.html#configuring-the-database Commented Oct 26, 2018 at 11:48
  • 1
    That's not how to use a controller, and there is no need for static instances like that. You should create a Symfony service instead. Commented Oct 26, 2018 at 13:19
  • 1
    @Almesia Not to be overly unkind but your approach is fundamentally flawed and most of the comments are missing the big picture. Your service is not a controller and should not extend from AbstractController. The get() on null" error message is because the container is not being injected. Which of course it won't because you don't have an actual controller. And the notion of making this a singleton is just wrong. Start over. Bring down a fresh project and follow the examples in the docs until you get a basic idea of how symfony works. Commented Oct 26, 2018 at 13:25
  • 1
    @Vyctorya This is a Symfony framework question and your hints are leading them down a completely wrong path. Commented Oct 26, 2018 at 13:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.