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)
Related
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
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.
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?
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
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?