Class xx was not found in the chain configured namespaces - symfony

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

Related

connect symfony with sqlite

Error:
my question may seems silly, but i don' t know to do that.
I created a symfony project and want to connect it to sqlite database.
Here is my doctrine config
I also set up the DATABASE_URL var.
However, when i want to generate migrations for example, i get the error could not find the driver.
I precise i installed sqlite by downloading a zip file.
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_sqlite'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
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
IF anyone knows how to do it, i would be very thankful :)
Before using sqlite in wamp, you should enable exension php_sqlite3.
Enable extension in wamp context menu, php extension -> php_sqlite3 and restart wamp
Either your driver isn't installed properly, run php -m to see if it appears in the list or you need to restart your web server (even if it's the embedded php-symfony web server)

cache drivers configuration

I am currently using Symfony with multiple entity managers. I did this accordingly to their example.
Everything works fine in dev and test environment. The connections are established and I can fetch, insert, etc my data as I want to. But then I came accross the problem that I seem to can't establish any connection to my databases in my prod environment: When I try to login it prints some standard error message like "You can not login because of some system problem". So I checked the logs: nothing. Maybe I need to adjust my verbosity level in prod? I couldn't find a way to get some kind of actual error message. Due to this reason I started comparing the configurations for the different environments. And there is a difference for doctrine on those three environments: test and dev don't use any file to overwrite some settings specified in the default doctrine configuration file. Prod does. And because I changed much stuff in my doctrine configuration file it seemed plausible that problem is burried right there. I deleted the additional doctrine configuration file for prod environment and it worked as expected again.
Here is my standard doctrine configuration file (dontrine.yaml):
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:
default_connection: admin
connections:
admin:
driver: 'pdo_mysql'
server_version: '5.7'
url: '%env(DB_ADMIN_URL)%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
mail:
driver: 'pdo_mysql'
server_version: '5.7'
url: '%env(DB_MAIL_URL)%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
host:
driver: 'pdo_mysql'
server_version: '5.7'
url: '%env(DB_HOST_URL)%'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
default_entity_manager: admin
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
admin:
connection: admin
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
Admin:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Admin'
prefix: 'App\Entity\Admin'
alias: Admin
mail:
connection: mail
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Mail:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Mail'
prefix: 'App\Entity\Mail'
alias: Mail
host:
connection: host
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Host:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Host'
prefix: 'App\Entity\Host'
alias: Host
This is my additional doctrine.yaml for prod environment:
doctrine:
orm:
metadata_cache_driver:
type: service
id: doctrine.system_cache_provider
query_cache_driver:
type: service
id: doctrine.system_cache_provider
result_cache_driver:
type: service
id: doctrine.result_cache_provider
services:
doctrine.result_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.result_cache_pool'
doctrine.system_cache_provider:
class: Symfony\Component\Cache\DoctrineProvider
public: false
arguments:
- '#doctrine.system_cache_pool'
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
doctrine.system_cache_pool:
adapter: cache.system
The configuration file for prod environment corresponds to the default one. I tried to add all these additional configurations once per entity manager as I did in the default configuration (for my multiple databases). But I didn't work. I don't actually know what this configuration is responsible for but I guess there is a reason why it has been added to default prod environment. I don't just want to delete it.
My questions are now:
How do I extend the additional configuration properly?
What is the sense in this configuration?

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)

symfony2.6 doctrine2 schema_filter parameter not exists

I am using symfony 2.6 (composer.json equal to its github repo) and I am trying to use the schema filter of DBAL.
in config.yml
# Doctrine Configuration
doctrine:
dbal:
schema_filter: ^sf2_
but error returned on shell:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized option "schema_filter" under "doctrine.dbal"
What am I missing?
EDIT:
config.yml (doctrine part only)
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database.driver%"
host: "%database.master.host%"
port: "%database.master.port%"
dbname: "%database.master.dbname%"
user: "%database.master.user%"
password: "%database.master.password%"
charset: UTF8
options:
1002: "SET NAMES 'UTF8'"
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
slaves:
slave1:
host: "%database.slave1.host%"
port: "%database.slave1.port%"
dbname: "%database.slave1.dbname%"
user: "%database.slave1.user%"
password: "%database.slave1.password%"
mapping_types:
enum: string
set: string
bit: boolean
types:
# some types
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
(POST) CONSIDERATION:
Anyway the schema_filter does not fit my requirements it is too vague to define with a reg exp (I mean for my actual schema is required a too complex reg exp and it is not pratical at all). I posted in doctrine2 groups a request for "enhancing" this option.
https://groups.google.com/forum/#!topic/doctrine-user/Tr4kkpIxwRk
What is your exact symfony version? I tried this config option with Symfony 2.6.5 and everything works fine.
Do you happen to have multiple connections? There is a note in documentation about this, at the end of the page:
Note that if you have multiple connections configured then the schema_filter configuration will need to be placed per-connection.
Manual tables
The schema_filter parameter is part of the Doctrine Migrations bundle. Do you have that installed?

Resources