cache drivers configuration - symfony

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?

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

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)

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 can I setup a second db connection with doctrine2 and symfony?

I use doctrine ORM in a Symfony 2.8 project.
My project contains several Bundles. For one Bundle, which generates Reports I want to use the db server with the slave replication as not to stress the master db server.
How to set this up?
What I tried so far:
In the config.yml
Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
slave:
driver: pdo_mysql
host: '%database_host_slave%'
port: '%database_port_slave%'
dbname: '%database_name_slave%'
user: '%database_user_slave%'
password: '%database_password_slave%'
charset: UTF8
Here I created my second db connection with the values stored in my parameters.yml.
I seem to get the orm configuration I tried to setup in the same file not correctly.
Let my first explain what I need:
I have a "ReportingBundle" which runs a console command. The entity manager is only needed to provide the proper authorization to the needed db server. The queries itself are oure SQL and I don't use the entities.
my service.yml for this bundle:
services:
myproject.reporting.service.csv_report_attachment:
class: Myproject\ReportingBundle\Service\DefaultCsvReportAttachmentService
arguments: ['#doctrine.orm.slave_entity_manager', '#logger', '#myproject.reporting.service.php_template_engine', 'reportingHtmlMailTemplate.php']
Now my non functioning orm setup in the config.yml:
ORM configuration
orm:
auto_generate_proxy_classes: '%kernel.debug%'
# naming_strategy: doctrine.orm.naming_strategy.underscore
default_entity_manager: default
entity_managers:
slave:
connection: slave
mappings:
MyprojectReportingBundle: ~
default:
connection: default
auto_mapping : true
metadata_cache_driver: redis
query_cache_driver: redis
result_cache_driver: redis
This results in
[Doctrine\ORM\ORMException]
Unknown Entity namespace alias 'MyprojectReportingBundle'.
I tried to follow the documentation here:
http://symfony.com/doc/current/reference/configuration/doctrine.html#custom-mapping-entities-in-a-bundle
Question:
What is the correct syntax, so that my query is run on the slave server instead of the default server?
I believe you have everything configured correctly. However, you need to make sure the Entity classes are defined in the Bundle namespace. So for example you'd want to have all the entities defined within Myproject\ReportingBundle\Entity in your example.

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