W
W
w_b_x2019-02-09 19:58:37
symfony
w_b_x, 2019-02-09 19:58:37

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);
//Дальше идёт код приложения

But I get an error:
Class "App\Entity\Account" is not a valid entity or mapped super class.
That being said App\Entity\Account is formed with php bin/console make:entity and works well from symfony controllers

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
padlyuck, 2019-02-09
@w_b_x

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.

A
Alexander Aksentiev, 2019-02-09
@Sanasol

  1. Create a composer package with shared entities
    There are also git submodules, but this is a more crutch option
    https://git-scm.com/book/en/v1/%D0%98%D0%BD%D1%81%...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question