symfony raw sql from table based on 2 server - php

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?

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

Symfony working with multiple db create Entity for a db

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.

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.

Symfony2 Doctrine ORM Manager named “db2” does not exist

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

Categories