I need to connect to an external database, I just need to do a simple query in one table so I think I dont need to create an new entity manager. I believe all configurations are set up correctly but I still don't get connected to the new database. So i´m missing something but cant find what, here are my files:
# Doctrine Configuration
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
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.root_dir%/data/data.db3'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
database2:
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
auto_mapping: true
parameters:
#Set_Goals Database
database_host: 127.0.0.1
database_port: null
database_name: set_goals
database_user: root
database_password: null
#Database2 Database
database2_host: 127.0.0.1
database2_port: null
database2_name: second
database2_user: root
database2_password: null
Repository:
public function getAccounts(){
$conn = $this->getEntityManager()->getConnection('database2');
$sql = 'SELECT * FROM leme_account';
$stmt = $conn->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
Error:
An exception occurred while executing 'SELECT * FROM leme_account':
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'set_goals.leme_account' doesn't exist
set_goals database is the default connection.
Thanks in advance and sorry if I´m missing something really simple here but I´m new to Symfony and programming in general and I followed the documentation and also some related questions here but can´t make it work.
I think you need to create another entity manager
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
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.root_dir%/data/data.db3'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
database2:
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
auto_mapping: true
default_entity_manager: default
entity_managers:
default:
connection: default
database2:
connection: database2
Then in your code
$em = $this->get('doctrine')->getManager();
$em2 = $this->get('doctrine')->getManager('database2');
The documentation mentions a mappings key but I don't know if it is mandatory, especially since you are using auto mapping.
Ref: https://symfony.com/doc/2.8/doctrine/multiple_entity_managers.html
Meanwhile, I found the problem.... it was a newb mistake. I was putting my method inside the repository of the other entity. When I added directly to the controller it worked.
Thanks for your answers.
Related
I am trying to work with 2 DBs and then 2 entity managers.
Here is my config doctrine part:
doctrine:
dbal:
default_connection: g2s
connections:
main:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
api:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: api
user: '%database_user%'
password: '%database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: main
auto_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore
api:
connection: api
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ApiBundle: ~
I ran the command "config:dump-reference" and the bundle is mapped.
But when trying to browse any page I got this error:
Unknown Entity namespace alias 'ApiBundle'
According to docs, the proper way is to explicitly set all mappings, and this is what I do in my project as well. https://symfony.com/doc/current/doctrine/multiple_entity_managers.html
There was a thread about this issue, and it should work for Symfony 2.6+ but same as #Cerad, I had no luck in enabling this. https://github.com/doctrine/DoctrineBundle/issues/60
I'm trying to connect a second database to my project in Symfony2. First, I added into parameters.yml some parameters to create the connection.
Then, I edited my config.yml, and now looks like:
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
circutor3:
driver: pdo_sqlsrv
host: "%database_host_circutor3%"
port: "%database_port_circutor%"
dbname: "%database_name_circutor%"
user: "%database_user_circutor3%"
password: "%database_password_circutor3%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
Finally, I tried to get connected, using the following code in my controller:
$em = $this->getDoctrine()->getManager('circutor3');
And, the error returned by Symfony2:
Doctrine ORM Manager named "circutor3" does not exist.
The circutor3 makes connection to a database, external to my system, so I don't need to create entities or objects. I only need to execute some SELECT to get information and store it using an array.
Is creating a typical mysqli connection the best way to solve my problem?
I don't know how to solve this with Symfony.
Thank you in advance.
you could access to the database connection in the controller as follow:
$connection = $this->getDoctrine()->getConnection('circutor3');
then use the connection as:
$stmt = $connection->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
Some help here and here
Hope this help
According to Symfony documentation (http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html), you only defined a connection, not an entity manager :
You have to create an entity_manager for each connection.
orm:
default_entity_manager: default
entity_managers:
default:
...
circutor3:
connection: circutor3
mappings:
AppBundle: ~
I was able to connect to doctrine in symfony2, this way:
# 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
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: false
proxy_namespace: Proxies
proxy_dir: Proxies
default_entity_manager: default # The first defined is used if not set
entity_managers:
default:
connection: default
mappings: # Required
ApplicationUserBundle:
type: annotation
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
dql:
datetime_functions:
UNIX_TIMESTAMP: DoctrineExtensions\Query\Mysql\UnixTimestamp
Now my question is that how to have this in configuration above?
$config->setSQLLogger(new Doctrine\DBAL\Logging\EchoSQLLogger());
I mean another way than this. As this is a $config method I guess that should be possible to have it in config.yml, but how?
You need to fill few config parameters
In doctrine.yaml config file enable logging
doctrine:
dbal:
logging: true
In services.yaml config file set logger class
services:
doctrine.dbal.logger:
class: Doctrine\DBAL\Logging\EchoSQLLogger
Do not forget to clean Symfony cache before usage php bin/console cache:clear
I'm following this guide on reverse engineering a database. The guide says it uses the paramters.yml file for the database parameters, but how can I override this? I have several connections listed in my config.yml, and I'd like to be able to choose one of them.
Use the --em parameter to specify which entity manager you want to use.
doctrine:mapping:convert [--filter="..."] [--force] [--from-database] [--extend[="..."]] [--num-spaces[="..."]] [--namespace[="..."]] [--em[="..."]] to-type dest-path
If you assign each Doctrine connection to its own Entity Manager, then you can specify the entity manager with the --em="entity_manager_name" flag. However, you will have to manually map each bundle to an entity manager. In the following example config, the other connection and entity manager are named customer
$ php app/console doctrine:mapping:convert yml ./src/Acme/CustomerBundle/Resources/config/doctrine/metadata/orm --em="customer" --from-database --force
config.yml
# 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
customer:
driver: %database_driver2%
host: %database_host2%
port: %database_port2%
dbname: %database_name2%
user: %database_user2%
password: %database_password2%
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AcmeDemoBundle: ~
AcmeStoreBundle: ~
customer:
connection: customer
mappings:
AcmeCustomerBundle: ~
I am trying to set up my symfony to use two databases connections. The problem is that Symfony won't map the Entities to the right EntityManager. So perform a query, I have to tell Symfony what manager it should use.
My config:
# 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
lookup:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "Lookup"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes
So, to query from my Lookup, i have to do: $this->getDoctrine()->getRepository('MbLookupBundle:Country', 'lookup');
Instead, I was hoping I could leave out the second parameter. This way, my bundle would be independent. The project manager can decide what database config he will implement in his project. He'll just have to make sure that the mapping is correct.
How does this work? What are the mappings used for, if this is not possible?
You have mistakes in connections naming :
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
# ...
mvibes: #This is the name of the connection
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
MbMyAppBundle: ~
mvibes:
mappings:
MbLookupBundle: ~
connection: mvibes #must refre to a connection's name defined above
Official doc : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html
A blog post I wrote about it : http://blog.alterphp.com/2011/10/configuration-trick-for-multiple-entity.html