Configure Second Entity Manager for Symfony5 Bundle Entities - symfony

I'd like to have 2 entity managers, one for a SQLite database for entities defined in a bundle, and the other for the main application. That way, I can load the data that never changes into one database, and load fixtures, tests, etc. into the application database. I'd expect the following to dump the sql for the bundle entities, but it doesn't:
bin/console doctrine:schema:update --dump-sql --em=geonames
[OK] No Metadata Classes to process.
Similarly, I would expect easyadmin to let me define classes to the bundle entities, but it also fails.
# easy_admin.yaml
entities:
Administrative:
class: Bordeux\Bundle\GeoNameBundle\Entity\Administrative
The configured class
"Bordeux\Bundle\GeoNameBundle\Entity\Administrative" for the path
"easy_admin.entities.Administrative" is no mapped entity.
I expect it has something to do with namespaces, or maybe the is_bundle parameter. I've spent a few hours hacking at this, following along with the tutorials and documentation about multiple entity managers, but I can't find anything that shows how to handle entities that come from a third-party bundle.
# doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
geonames:
url: '%env(DATABASE_GEONAMES_URL)%'
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
geonames:
connection: geonames
mappings:
BordeuxGeoNameBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'Geonames\Entity'
alias: Geonames

I have not configured multiple entity managers for the latest and greatest Symfony versions so I setup a little test case and came up with this for a configuration:
orm:
default_entity_manager: default
auto_generate_proxy_classes: true
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
geonames:
connection: geonames
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
BordeuxGeoNameBundle:
is_bundle: true
type: annotation
dir: 'Entity'
prefix: 'Bordeux\Bundle\GeoNameBundle\Entity'
alias: GeoNames
I tested it using:
bin/console doctrine:mapping:info --em=geonames
And confirmed the entities were being mapped. I did not install EasyAdmin and test it but I don't see any reason why it would not work.
The main difference was using the entity namespace for the prefix attribute.
And just for my own future reference, I committed the test project to github.

Related

Class xx was not found in the chain configured namespaces

I have two databases for my application so I put the entities in two separate folders to simplify things.
Entity/
Local/
User.php
Foo.php
Remote/
Bar.php
but I have an error 500 that I don't understand at the time of connection
[2020-09-03 17:10:58] request.CRITICAL:
Uncaught PHP Exception Doctrine\Persistence\Mapping\MappingException: "The class 'App\Entity\Local\User' was not found in the chain configured namespaces " at /www/myapp/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php line 23 {"exception":"[object] (Doctrine\\Persistence\\Mapping\\MappingException(code: 0): The class 'App\\Entity\\Local\\User' was not found in the chain configured namespaces at /www/myapp/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23)"} []
Here is my doctrine.yml configuration
doctrine:
dbal:
default_connection: local
connections:
local:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
remote:
url: '%env(resolve:DATABASE_URL_MS)%'
charset: 'UTF-8'
wrapper_class: App\Connections\ConnectionRemote
mapping_types:
timestamp: string
xml: string
schema_filter: $sales$
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: local
entity_managers:
local:
connection: local
mappings:
local:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Local'
prefix: 'App\Entity\Local'
alias: local
AnotherBundle: ~
remote:
connection: remote
mappings:
remote:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Remote'
prefix: 'App\Entity\Remote'
alias: remote
Even stranger, if I refresh my page, I am well connected and the error doesn't come back...
Even crazier, it works very well in dev mode
I think there's a bug in Doctrine, it's the fact that it works in dev and not in prod that put me on the track of the solution.
I had dumped the dev and prod conf to compare the two versions and what I noticed was that doctrine.orm.default_entity_manager was set to default even though it is set to custom in package/doctrine.yaml.
so for the value to be really taken into account you must also set doctrine.orm.default_entity_manager in prod/doctrine.yaml
doctrine:
orm:
default_entity_manager: local

Problems while using multiple databases

Background: I am developing a web application with Symfony5 for a customer. The application should be available for several customers. Each customer should get his own file directory (for pdf etc.) and database. The file-directory is not the problem, but I can't get anywhere with the databases.
According to the Symfony docu you can work with different DBs. I configured this in the config/packages/doctrine.yaml.
doctrine:
dbal:
connections:
Client1:
dbname: client1
host: localhost
port: 3306
user: admin
password: secret_pw
driver: pdo_mysql
server-version: 5.7
Client2:
dbname: client2
host: localhost
port: 3306
user: admin
password: secret_pw
driver: pdo_mysql
server-version: 5.7
orm:
auto_generate_proxy_classes: true
entity_managers:
Client1:
connection: Client1
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Client1:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Client1
Client2:
connection: Client2
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
mappings:
Client2:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Client2
So far it also works, if I create the database from the CLI and execute the migrations.
But if I want to read e.g. a user from the DB for Client2 (is set in $dbClient) with
$userRepo = $this->getDoctrine()
->getRepository(User::class, $dbClient);
$user = $userRepo->findOneByeEmail($this->userEmail);
the DB for Client1 is ALWAYS set in the repo :((.
Did I do something wrong in the config?
Or is there something else that needs to be configured elsewhere?
Thank you very much for your help
Frank
Both entity managers have the same prefix in your mappings. You should try something like
prefix: 'App\Entity\Client1'
//...
prefix: 'App\Entity\Client2'
That's what they do in the doc.
You inject wrong entity manager
$this->getDoctrine()->getManager('Client2')>getRepository(User::class)->findOneByeEmail($this->userEmail);

Symfony Doctrine config custom types not taken into account after cache is built

Hello I have a strange behavior with Doctrine on Symfony4 (4.0.9), in the console, after the cache was generated by a first run of some command (in a dev environment) the custom types defined in config/packages/doctrine.yml are no longer taken into account.
Why is this happening and how can I fix it ?
Other informations that may be useful: I copied the app code (controllers, entities, etc.) from a Symfony2 application and imported it in a new Symfony 4 app as I'm in the process of making it compatible with Symfony 4. Also, I don't have this behavior on the web version of the app (the custom types are always recognized)
# config/packages/doctrine.yml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
types:
enumprofilecompletionstep: 'App\DBAL\EnumProfileCompletionStepPossibleValuesType'
enumsection: 'App\DBAL\EnumSectionType'
enumsex: 'App\DBAL\EnumSexType'
enumstatus: 'App\DBAL\EnumStatusType'
phone_number: 'Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
Thanks for your time,
MrPOC

How to configure Doctrine to use yaml mapping on Symfony 4

I'm new to Symfony 4
I use Doctrine an I want to use yaml entity mapping.
So i configured the file doctrine.yaml and change type:annotation to type:yml.
And when I tried php bin/console make:entity, there is no yaml mapping file generated linked to this entity
this is my doctrine.yaml file:
parameters:
# Adds a fallback DATABASE_URL if the env var is not set.
# This allows you to run cache:warmup even if your
# environment variables are not available yet.
# You should not need to change this value.
env(DATABASE_URL): ''
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
# With Symfony 3.3, remove the `resolve:` prefix
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: yml
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
An example of mapping following your needs:
Change the file name including the "orm" text doctrine.orm.yaml, and take a look at the dir option in the example below (that follows your needs):
App:
is_bundle: false
type: yml
# "dir" in this case must be pointed where are stored your doctrine files (can be anywhere inside the project dir)
dir: "%kernel.project_dir%/config/doctrine"
prefix: App\Entity
Reference: Doctrine yaml mapping (v2.6 current)

Symfony 2 - doctrine:mapping:import to custom namespace

I'm just getting started with Symfony2, currently trying to generate Doctrine entities from an existing database. I'm following the official tutorial on the topic, however, I'd like to have my entities generated in a different namespace other than the default (MyVendor\MyBundle\Model\Entity instead of MyVendor\MyBundle\Entity).
I've edited the config.yml file like this:
doctrine:
orm:
entity_managers:
default:
mappings:
MyBundle:
prefix: MyVendor\MyBundle\Model\Entity
dir: Model/Entity
Unfortunately, I can't get this to work, as entities are still generated in the default namespace and directory. What am I doing wrong?
Change above code as following: Make auto_mapping true...
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
auto_mapping: true
mappings: ~
Try this.. Hope this will work for you.

Resources