Answer the question
In order to leave comments, you need to log in
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']
];
}
}
<?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();
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:
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question