M
M
Maxim Lagoysky2019-05-23 10:12:54
symfony
Maxim Lagoysky, 2019-05-23 10:12:54

How to describe entities for another base?

Hello everyone, in short, I need to create dynamic connections in the database, the connection itself turned out to be done, but I still don’t really understand how to deal with entities and repositories.
Create a dynamic connection to the database

like this (code simplified)
$params['user'] = 'qwerty';
$params['host'] = 'localhost';
$params['port'] = 3306;
$params['dbname'] = 'test';
$params['password'] = 'test';
$params['url'] = 'mysql://qwerty:[email protected]:3306/test';

$connection = EntityManager::create(
    $params,
    $this->em->getConfiguration(),
    $this->em->getEventManager()
);

$this->em это дефолтное подключение

then I create an entity in
src/Entity/Test/MyTable

and make a selection
$connection->getRepository(MyTable::class)->findAll();

and everything seems to be working great, but I remember that I need a repository for this entity, I add

/**
* @ORM\Entity(repositoryClass="App\Repository\Test\MyTableRepository")
* @ORM\Table(name="my_table")
*/

and immediately I catch an error that such a table does not exist in this database, but in the error it is clear that he is looking for a table in the default database, how to fix this? maybe something else needs to be added in creating a new connection? Or create a new connection in general somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2019-05-23
@skobkin

Symfony has documentation on this topic: https://symfony.com/doc/current/doctrine/multiple_... And you can see an
example from real life, for example, in my mini-project. There PostgreSQL and SQLite work at the same time. Attention should be paid to . This, of course, is for previously known compounds. Well, for entities from each database, use your own EntityManager. This is in any case - regardless of whether the connections are defined in advance or at runtime.
Most likely, you are using the wrong EntityManager or creating it with the wrong connection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question