V
V
Vitaliy2021-07-31 13:42:31
PHP
Vitaliy, 2021-07-31 13:42:31

How to convert to bootstrap.php and cli-config.php classes when setting up Doctrine ORM?

Judging by the documentation of the Doctrine, the settings do not use classes, namely the bootstrap.php and cli-config.php files, which is not convenient to call require "bootstrap.php" to use the $entityManager object in the future. Therefore, I decided to convert these files into a trace. classes :

DoctrineBootstrap:

<?php

namespace App\Helpers\Doctrine;

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Dotenv\Dotenv;

class DoctrineBootstrap
{
    private $isDevMode;
    private $proxyDir;
    private $cache;
    private $useSimpleAnnotationReader;
    protected EntityManager $entityManager;

    /**
     * @throws \Doctrine\ORM\ORMException
     */
    public function __construct()
    {
        $this->entityManager = EntityManager::create($this->getConnection(), $this->getDoctrineConfig());
    }

    public function getDoctrineConfig()
    {
        $this->isDevMode = true;
        $this->proxyDir = null;
        $this->cache = null;
        $this->useSimpleAnnotationReader = false;

        return Setup::createAnnotationMetadataConfiguration(
            array('src/Entity/'),
            $this->isDevMode,
            $this->proxyDir,
            $this->cache,
            $this->useSimpleAnnotationReader
        );
    }

    public function getConnection(): array
    {
        $dotenv = Dotenv::createImmutable(__DIR__ . '/src');
        $dotenv->load();

        return [
            'dbname' => $_ENV['DB_NAME'],
            'user' => $_ENV['DB_USER'],
            'password' => $_ENV['DB_PASSWORD'],
            'host' => $_ENV['DB_HOST'],
            'driver' => $_ENV['DB_DRIVER']
        ];
    }
}


cliconfig:
<?php

namespace App\Helpers\Doctrine;

use Doctrine\ORM\Tools\Console\ConsoleRunner;

class CliConfig extends DoctrineBootstrap
{
    public function __construct()
    {
        parent::__construct();

        ConsoleRunner::createHelperSet($this->entityManager);
    }
}

return new CliConfig();


When calling CliConfig () in fact, which should work when working with the console, an error is displayed:
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:


Perhaps someone has implemented something similar using classes instead of the usual file settings?

Can anyone help?
Thanks to.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question