Error : InvalidConfigurationException: Unrecognized options "auto_mapping" under "doctrine.orm"
This is my config.yml file code
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
symfonydb:
driver: pdo_mysql
host: localhost
port: null
dbname: symfony
user: root
password: null
charset: 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%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
default_entity_manager: default <<<<<<<<<<
entity_managers:
default:
connection: default
mappings:
AcmeDemoBundle: ~
AcmeUserBundle: ~
symfonydb:
connection: symfonydb
mappings:
FooNewsBundle: ~ <<<<<<<<<
If I remove code which is after auto_mapping: true, then it works fine and not throw any error.
So what is the problem?
Thnaks.
Have a look at this GitHub issue: Problem with installation?
The auto_mapping entry goes under the entity_managers.default node, not on the root of the orm node.
Related
I think all parameters are set properly but when I run command of
php bin/console doctrine:schema:update --force --em=archive
it throws an error:
Doctrine ORM Manager named "archive" does not exist.
So I think I might have a syntax problem that I cannot find.
My code:
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
archive:
driver: pdo_mysql
host: '%database2_host%'
port: '%database2_port%'
dbname: '%database2_name%'
user: '%database2_user%'
password: '%database2_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Default:
mapping: true
type: annotation
dir: '%kernel.root_dir%/../src/App/Base/Entity'
alias: 'Default'
prefix: 'Default\Base\Entity'
is_bundle: false
Archive:
mapping: true
type: annotation
dir: '%kernel.root_dir%/../src/App/Base/Entity/Archive'
alias: 'Archive'
prefix: 'App\Base\Entity\Archive'
is_bundle: false
Also I set parameters.yml correctly, too.
I have a project that uses two databases. The Doctrine section of Config.yml bundle is setup as follows.
doctrine:
dbal:
default_connection: db1
connections:
db1:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
db2:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: em1
entity_managers:
em1:
connection: db1
mapping:
FirstBundle: ~
em2:
connection: db2
mappings:
SecondBundle: ~
All the parameters are setup in the parameters.yml file correctly.
whenever I run any $ php bin/console commands I get the following error
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The attribute "name" must be set for path "doctrine.orm.entity_managers.em2.mappings".
Are there any settings that I'm missing?
I notice your em1 section is set to mapping instead of mappings. Also, in my working config, I set default_entity_manager to the connection value. So maybe try making these changes:
orm:
default_entity_manager: db1
entity_managers:
em1:
connection: db1
mappings:
FirstBundle: ~
em2:
connection: db2
mappings:
SecondBundle: ~
That may not be the problem, but try it.
I'm using this doctrine config for prod environment:
doctrine:
dbal:
default_connection: app_connection
connections:
app_connection:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
bit: boolean
But when working locally, I always have to delete the mapping_types:bit: boolean part in order to be able to drop and create my database. (Otherwise I get error 'unknown database')
So I thought I would override the doctrine config in the config_dev.yml file by copying it and not writing the mapping_types section, but it's not working. If I don't delete it in the config.yml, I keep getting the error.
I guess I could use a config_dev file that would not import the config file, but I was wondering if there was an effective way to override the 'mapping_types' section.
You could use a parameters field (from the parameters.yml files) in order to configure in different machine. As example, you could specify in the config.yml files:
doctrine:
dbal:
default_connection: app_connection
connections:
app_connection:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types: "%database_mapping_types%"
And configure in your parameters.yml of the production server:
parameters.yml
database_mapping_types: {bit: boolean}
And configure in your parameters.yml of the local server:
parameters.yml
database_mapping_types: ~
obviously set a default in the parameter.yml.dist as example:
parameters.yml.dist
database_mapping_types: ~
EDIT:
Of course, you can specify a different configuration for prod environment instead of the dev as follow:
config_dev.yml
doctrine:
dbal:
default_connection: app_connection
connections:
app_connection:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types: ~
And in the prod environment you can override the key you need as follow:
config_prod.yml
doctrine:
dbal:
default_connection: app_connection
connections:
app_connection:
mapping_types:
bit: boolean
Hope this help
I create YML entity, generate entity and config multiple connections
config.yml
# Doctrine Configuration
doctrine:
dbal:
default_connection: db1
connections:
db1:
driver: '%database_driver%'
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
db2:
driver: '%database_driver2%'
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
my orm.yml
TestBundle\Entity\Test:
type: entity
table: Test
id:
id:
type: integer
nullable: false
options:
unsigned: true
id: true
fields:
name:
type: string
nullable: false
length: 255
options:
fixed: false
lifecycleCallbacks: { }
after clean cache and generate entities, I try to run this code (in command controller)
<?php
...
protected function execute(InputInterface $input, OutputInterface $output)
{
$doctrine = $this->getContainer()->get('doctrine')->getManager('db2');
//both methods cant work :( db2_entity_manager not found
$doctrine = $this->getContainer()->get('doctrine.orm.db2_entity_manager');
$test = $doctrine->getRepository('DionisDataBaseBundle:Test');
}
And I have error:
[InvalidArgumentException]
Doctrine ORM Manager named "db2" does not exist.
change config.yml to
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: db1
entity_managers:
db1:
connection: db1
mappings:
DionisDataBaseBundle: ~
db2:
connection: db2
mappings:
DionisDataBaseBundle: ~
db3:
connection: db2
mappings:
DionisDataBaseBundle: ~
you just declared connections but no entity manager, from one of my projects :
doctrine:
dbal:
default_connection: default
connections:
default:
host: "%database_host%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
mapping_types:
enum: string
string: string
# schema_filter: ~^(sf_fos_user)~
prod:
host: *****
dbname: ****
user: "%database_user%"
password: "%database_password%"
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
mapping_types:
enum: string
string: string
# schema_filter: ~^(sf_fos_user)~
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
Bundle2: ~
db2:
connection: prod
mappings:
otherBundle: ~
auto_generate_proxy_classes: "%kernel.debug%"
I'm working with a Symfony3 app and I want to set up multiple connections to different databases.
I've been looking around and I found out the documentation about entityManagers and DB connections. My config.yml is configured as follows:
config.yml
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
mapping_types:
enum: string
other:
driver: pdo_mysql
host: "%database_host2%"
port: "%database_port2%"
dbname: "%database_name2%"
user: "%database_user2%"
password: "%database_password2%"
charset: UTF8
mapping_types:
enum: string
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
So now I do can access to my database like this:
$con2 = $this->get('doctrine.dbal.other_connection');
$orders = $con2->fetchAll('SELECT * FROM orders');
But what I really need is to configure a second orm-mapping connection which will allow me to interact with entities instead of dealing with the second database directly. So again as the documentation says I added under the doctrine orm label:
orm:
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
other:
connection: other
mappings:
OtherBundle: ~
This throws an exception:
ParseException in Parser.php line 296:
Unable to parse at line 78 (near " entity_managers:").
How should I configure my config.yml to allow orm-mapping for my second database connection? Should I delete the dql label and use it only under a certain entity manager label?
check the doc here for full reference of the configuration about doctrine.
Check the indentation of the dql function etc.
Probably us are using the Shortened Configuration Syntax where all options available can be placed directly under doctrine.orm config level.
Hope this help
Try this one:
doctrine:
orm:
auto_generate_proxy_classes: true
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
other:
mappings:
OtherBundle: ~
I solved the problem. Thanks for the answers because they were pretty much the solution. I just had to use the labels in their correct levels.
Just in case it can be useful for someone, I'll post what I've done:
config.yml
orm:
entity_managers:
default:
mappings:
AppBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
dql:
string_functions:
DAY: DoctrineExtensions\Query\Mysql\Day
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
other:
mappings:
OtherBundle: ~
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_generate_proxy_classes: "%kernel.debug%"
Once the (other) entity manager is created you just can use it within a controller as you'd do with the default em just by specifying the entity manager name you are using.
$orders = $this->get('doctrine')
->getRepository('OtherBundle:Orders', 'other')
->findAll();
Now you're ready to go.
Thanks for your help.