Answer the question
In order to leave comments, you need to log in
How to use Doctrine outside of Symfony?
Hello!
There is a project on Symfony and there is a separate php application.
I would like to use common Entities to interact with the database, but not put the application in the Symfony structure. How to implement it correctly?
The php application is located in the source folder
(full path _project_/src/worker/application.php).
Tried to do like this:
require __DIR__.'/../../vendor/autoload.php';
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use App\Entity\Account; // Подключаю объект account, который прекрасно работает из Symfony
$isDevMode = true;
$entitiesPaths = array(__DIR__.'/../Entity');
$dbParams = array(
'dbname' => '_база_',
'user' => '_логин_',
'password' => '_пароль_',
'host' => '127.0.0.1',
'driver' => 'pdo_mysql',
);
$config = Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode);
$em = \Doctrine\ORM\EntityManager::create($dbParams, $config);
$id = 9; // для примера
$account = $em->getRepository(Account::class)->findOneBy([
'id'=>$id,
]);
var_dump($account);
//Дальше идёт код приложения
Answer the question
In order to leave comments, you need to log in
Setup::createAnnotationMetadataConfiguration($entitiesPaths, $isDevMode, null, null, false);
for doctrine up to version 3.0 (in the third version, it seems to be done according to normal). Most likely, the problem is that in a Symfony project you write something like in the annotations @ORM\Entity
, and for this, a pure doctrine needs to slip a normal AnnotationReader, and not Simple, which is created by default.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question