Symfony2 Doctrine ORM Manager named “db2” does not exist - php

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%"

Related

Symfony 4 sqlite database creation for test environment

For integration test purpose I am trying to create an sqlite db for test env but when I run php bin/console doctrine:schema:create --env=test I get errors that tables already exists. I suppose because it is not really creating a new test db but going on the existing db.
Looks like it is not reading from env.test.
I created a doctrine.yaml in config/test like this:
doctrine:
dbal:
driver: 'pdo_sqlite'
url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'
What I am missing to create an sqlite test db?
Strange thing I get in the error:
In PDOConnection.php line 90:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user_menu' already exists
Why SQL? Should not be IT for sqlite, am I right?
In config/packages/doctrine.yaml, I have:
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
connections:
gui:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_GUI_URL)%'
upv6:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_UPV6_URL)%'
orm:
auto_generate_proxy_classes: true # <- change to true
proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
proxy_namespace: Proxies
entity_managers:
gui:
connection: gui
mappings:
Gui:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Gui'
prefix: 'App\Entity\Gui'
alias: Gui
upv6:
connection: upv6
mappings:
Upv6:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Upv6'
prefix: 'App\Entity\Upv6'
alias: Upv6
My doctrine settings:
doctrine:
dbal:
connections:
gui:
driver: pdo_mysql
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_GUI_URL)%'
host: localhost
port: null
user: root
password: null
logging: true
profiling: true
options: { }
mapping_types: { }
slaves: { }
shards: { }
upv6:
driver: pdo_mysql
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_UPV6_URL)%'
host: localhost
port: null
user: root
password: null
logging: true
profiling: true
options: { }
mapping_types: { }
slaves: { }
shards: { }
default:
driver: pdo_sqlite
url: '%env(resolve:DATABASE_URL)%'
host: localhost
port: null
user: root
password: null
logging: true
profiling: true
options: { }
mapping_types: { }
default_table_options: { }
slaves: { }
shards: { }
default_connection: default
types: { }
orm:
auto_generate_proxy_classes: true
proxy_dir: /var/www/symfony/var/cache/test/doctrine/orm/Proxies
proxy_namespace: Proxies
entity_managers:
gui:
connection: gui
mappings:
Gui:
is_bundle: false
type: annotation
dir: /var/www/symfony/src/Entity/Gui
prefix: App\Entity\Gui
alias: Gui
mapping: true
query_cache_driver:
type: array
namespace: null
cache_provider: null
metadata_cache_driver:
type: array
namespace: null
cache_provider: null
result_cache_driver:
type: array
namespace: null
cache_provider: null
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
default_repository_class: Doctrine\ORM\EntityRepository
auto_mapping: false
naming_strategy: doctrine.orm.naming_strategy.default
quote_strategy: doctrine.orm.quote_strategy.default
entity_listener_resolver: null
repository_factory: doctrine.orm.container_repository_factory
hydrators: { }
filters: { }
upv6:
connection: upv6
mappings:
Upv6:
is_bundle: false
type: annotation
dir: /var/www/symfony/src/Entity/Upv6
prefix: App\Entity\Upv6
alias: Upv6
mapping: true
query_cache_driver:
type: array
namespace: null
cache_provider: null
metadata_cache_driver:
type: array
namespace: null
cache_provider: null
result_cache_driver:
type: array
namespace: null
cache_provider: null
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
default_repository_class: Doctrine\ORM\EntityRepository
auto_mapping: false
naming_strategy: doctrine.orm.naming_strategy.default
quote_strategy: doctrine.orm.quote_strategy.default
entity_listener_resolver: null
repository_factory: doctrine.orm.container_repository_factory
hydrators: { }
filters: { }
resolve_target_entities: { }
The issue is caused by the way Symfony merges configuration files.
/config/packages/*.yml > /config/packages/<env>/*.yml
This results in all of the connections and entity managers defined in packages/doctrine.yml being added to your packages/test/doctrine.yml
To see the merged configuration settings Symfony will use, run:
php bin/console --env=test debug:config doctrine
Because of this running bin/console --env=test doctrine:schema:create will attempt to create the schema for ALL of the entity managers present in the resulting configuration.
To resolve the issue you will need to segregate your environment configs into prod, test, and dev or alternatively use .env.test to change the URLs used by Syfmony.
Depending on how you need to access your entity managers
Example
This is only an example of how to segregate the environment
configurations, be sure to change any needed values for your application
requirements
config/packages/doctrine.yml
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:
orm:
auto_generate_proxy_classes: true # <- change to true
proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
proxy_namespace: Proxies
config/packages/dev/doctrine.yml
doctrine:
dbal:
# configure these for your database server
connections:
gui:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_GUI_URL)%'
upv6:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_UPV6_URL)%'
entity_managers:
gui:
connection: gui
mappings:
Gui:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Gui'
prefix: 'App\Entity\Gui'
alias: Gui
upv6:
connection: upv6
mappings:
Upv6:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Upv6'
prefix: 'App\Entity\Upv6'
alias: Upv6
config/packages/test/doctrine.yml
doctrine:
dbal:
driver: 'pdo_sqlite'
url: 'sqlite:///%kernel.project_dir%/var/data/test.sqlite'
#DEFINE THE ENTITY MANAGERS TO USE THE default CONNECTION
#orm:
#since the connections are not the same
#you need to define your entity managers here...
#entity_managers:
# gui:
# connection: default #<---- NOTICE DEFAULT and not gui
# (not sure what entity managers are needed for sqlite)
#...
config/packages/prod/doctrine.yml
imports:
- { resource: '../dev/doctrine.yaml' }
doctrine:
orm:
auto_generate_proxy_classes: false #<-- change to false
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
#...

Doctrine ORM Manager named "" does not exist - syntax error

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.

error The attribute "name" must be set for path "doctrine.orm.entity_managers.db2.mappings" with multiple databases

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.

Symfony3: How to set multiple connections?

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.

Symfony2 : Error with entitymanager configuration

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.

Categories