Multiple connections with different entities in same Symfony bundle - php

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

Related

Create Symfony Entity with multiple Entity-Managers

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

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.

Symfony2 : Doctrine - Update table structure on two different entity managers

I have two servers A and B and some mapped entity to Server A and another to server B.
When I execute doctrine:schema:update all tables are created on the B server. Any idea why this might be happening?
Here my config.yml file
# Doctrine Configuration
doctrine:
dbal:
default_connection: A
connections:
A:
driver: pdo_sqlsrv
port: 1433
host: A
dbname: MADB
user:
password:
charset: UTF8
B:
driver: pdo_sqlsrv
host: B
port: 1433
dbname: MADB2
user:
password:
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: B
entity_managers:
labete:
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: false
connection: A
mappings:
HMAdminBundle: ~
HMMainBundle: ~
main:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: B
mappings:
HMProfBundle: ~
HMMainBundle: ~
HMAdminBundle: ~
Your configuration indicates that your default entity manager is B. That's why when you run the doctrine command, tables are generated for B.
When you run doctrine commands, there's usually an entity manager option that you can pass in:
doctrine:schema:update --em=A
You can see all the options by running:
doctrine:schema:update --help

Generate entities using xml mappings using command doctrine:genereate:entities to a different location other than the default location

I am using symfony2 with doctrine.
Here is my parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: ~
database_name: somedb
database_user: root
database_password: ~
Here is my config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
dbname: %database_name%
host: %database_host%
port: %database_port%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: false
mappings:
name:
type: xml
dir: %kernel.root_dir%/../src/bundlename/Resources/config/doctrine
prefix: bundlename\Model\Entity
is_bundle: false
Running doctrine:mapping:import generates the xml mappings files in the following directory
*bundlename\Resources\config\doctrine*
Running doctrine:generate:entities gives the following error:
Generating entities for bundle "bundlename"
[Doctrine\Common\Persistence\Mapping\MappingException]
Invalid mapping file 'bundlename.Model.Entity.some_entity
.orm.xml' for class 'bundlename\Model\Entity\some_entity'
.
Using auto_mapping: true correctly generates the entities at bundlename\Entity*
What I want to accomplish here is to instruct doctrine to generate entities at a different location other than the default location

Working with two entity managers in the same bundle in Symfony2

I'm trying to work with two entity managers for the same bundle. My configuration is like this:
orm:
default_entity_manager: default
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Is there any way to tell which entities belong to which entity manager? It crashes now if I want to work with a table which doesn't belong to the default entity manager.
UPDATE
here is my configuration for the connection:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
default_entity_manager: electra
entity_managers:
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
For using multiple entitymanager in same bundle you have to config mapping options for each entitymanager.
http://symfony.com/doc/current/reference/configuration/doctrine.html
Exemple off config file
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
second:
driver: %database_sqlite_driver%
host: ~
port: ~
dbname: %database_sqlite_shop_name%
path: %database_sqlite_shop_name%
user: ~
password: ~
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
YourBundle:
# you must specify the type
type: "annotation"
# The directory for entity (relative to bundle path)
dir: "Entity/FirstDb"
#the prefix
prefix: "Your\Bundle\Entity\FirstDb"
shop:
connection: second
mappings:
YourBundle:
type: "annotation"
#here the second path where entity for the connection stand
dir: "Entity/SecondDb"
#the prefix
prefix: "Your\Bundle\Entity\SecondDb"
You can now use console for managing your db with the --em parameter
Ex : update database for shop entitymanager
php app/console doctrine:schema:update --em=shop
Read mapping information from Your\Bundle\Entity\SecondDb
Ex : update database for default entitymanager
php app/console doctrine:schema:update
Read mapping information from Your\Bundle\Entity\FirstDb
Ok. Tried to edit your original post but it's waiting for peer review. Not sure how long that takes. Try changing your config to:
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
# Make an explicit connection just for clarity
old_project:
dbname: old_project
user: root
password: 123123
host: 1.1.1.1
port: 1
electra:
dbname: electra
user: root
password: 123123
host: 2.2.2.2
port: 2
orm:
# Humor me and add these
auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
default_entity_manager: electra
entity_managers:
# Make an explicit old_project em so default does not confuse us
old_project:
connection: old_project
mappings:
XXDemoBundle: ~
electra:
connection: electra
mappings:
XXDemoBundle: ~
default:
connection: default
mappings:
XXDemoBundle: ~
Now completely blow away your cache just to be sure then run:
php app/console doctrine:mapping:info --em electra
php app/console doctrine:mapping:info --em old_project
You should get identical results. I tested this on my system so I'm fairly certain that if you don't then you have some typo somewhere.
So mapping info is working. Next step is to verify that both databases match your entity schema. So do this:
php app/console doctrine:schema:update --em electra --dump-sql
php app/console doctrine:schema:update --em old_project --dump-sql
Neither should produce any output. If one does then it means your database does not match your entities and that needs to be resolved (possibly using the --force option) before queries will work.
Once the databases are in sync then you should probably use doctrine:query:dql and do a test query against both managers. Then go back into your code.
=========================================
It has now been understood that the real goal is to have two entity managers point to the same set of entities but somehow indicate that each entity manager should limit itself to a certain set of those entities. And that is not something the S2 supports out of the box.
You could look through the Doctrine manual and see how it handles the entity metadata and maybe do something with that but that could get complicated.
The only thing that S2 really offers is the ability to bind an entity manager to all the entities in one or more bundles using the mapping attribute. If you wanted to share say three of seven entities from one bundle with another bundle then you would simply recreate those entities in the second bundle. Possibly by extending the class so as to avoid code duplication.
I think you might want to alter your approach a bit. If you have a set of core entities shared with multiple bundles then put those in their own bundle. Each follow on bundle can then add additional entities.

Categories