I have a Symfony project with the doctrine.yaml file below. So I have two entity managers (default and user) that connect my two databases. But how do I create a new entity now?
# config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '8'
charset: utf8mb4
user:
# configure these for your database server
url: '%env(resolve:DATABASE2_URL)%'
driver: 'pdo_mysql'
server_version: '8'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Admin:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Admin'
prefix: 'App\Entity\Admin'
alias: Admin
user:
connection: user
mappings:
User:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/User'
prefix: 'App\Entity\User'
alias: User
With make:entity there is now an error, because this command only works with one entity manager (please correct me if it works somehow).
Related
I'm trying to work with multiple db in symfony I followed the documentation: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html . I had some Entities creates and now I don't know how to use them.
My doctrien.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: 'utf8mb4'
customers:
url: '%env(resolve:DATABASE_CUSTOMERS)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: 'utf8mb4'
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
auto_mapping: true
customers:
connection: customers
mappings:
Lacoste:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customers'
prefix: 'App\Entity\Customers'
alias: Customers
If i pass the entity to the folder I want gives this error
Warning: include(/Applications/XAMPP/xamppfiles/htdocs/project/vendor/composer/../../src/Entity/Company.php): failed to open stream: No such file or directory.
I have 2 database server so I defined my doctrine in following way:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: 'pdo_mysql'
server_version: 'mariadb-10.3.16'
charset: utf8
default_table_options:
charset: utf8
collate: utf8_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
customer:
driver: 'pdo_mysql'
server_version: 'mariadb-10.3.16'
charset: utf8
default_table_options:
charset: utf8
collate: utf8_unicode_ci
url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
orm:
auto_generate_proxy_classes: true
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
Now I need to make the query from table inside customer server:
$qb = $this->connection->createQueryBuilder()
->select(*)
->from('customer.tbl_user')
I am not being able to run this query. Can anybody help me how can I set up this multiple database connections?
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
#...
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'm developing a new project with Symfony that will have a website and an admin site in the same bundle. I have 2 different connections (one for the data database and one for the admin database) and I wanted to be able to distinguish between entities so for that I have this config.yml:
doctrine:
dbal:
default_connection: project
connections:
project:
driver: pdo_pgsql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
backoffice:
driver: pdo_pgsql
host: '%backoffice_database_host%'
port: '%backoffice_database_port%'
dbname: '%backoffice_database_name%'
user: '%backoffice_database_user%'
password: '%backoffice_database_password%'
charset: UTF8
orm:
default_entity_manager: project
entity_managers:
project:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: project
auto_mapping: true
mappings:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/project"
#the prefix
prefix: "Project\\ProjectBundle\\Entity\\Project"
backoffice:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: backoffice
mappings:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/Backoffice/"
#the prefix
prefix: "Project\\ProjectBundle\\Entity\\Backoffice"
Now I'm trying to configure FOSUserBundle and when I want to update the schema for the backoffice database I get the following error: The class 'Project\ProjectBundle\Entity\Backoffice\User' was not found in the chain configured namespaces Project\ProjectBundle\Entity\Project, FOS\U
serBundle\Model. The FOSUserBundle configuration is:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: backoffice
user_class: Project\ProjectBundle\Entity\Backoffice\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
Is there any way to achieve that? Another solution that I thought of is having a Bundle with only the project database entities, but I don't know if that would be too complex.
Thanks