I am using DBAL with Doctrine to make a connection in Symfony 3 to a database. What I'd like to do is to have all my queries in a PHP file, not in a controller.
In a controller I can use this:
$conn = $this->get('doctrine.dbal.database2_connection');
to get a connection, but in a simple PHP I cannot.
So, I don't know how to call multiple connections without using the EntityManager
object because I'm not working with an Entity class.
If you use PDO, every connection is treated as an object, and you can have an arbitrary number of them simultaneously. Check out the docs
To achieve what you want to need to master the next things:
You need to check how to work with multiple databases (I guess that's what you mean by many connections) in the documentation .
Also, you need to see how to create custom repositories.
You will need to use DQL (doctrine query language) to do your own queries.
And the last point is that you need to get used with Symfony's dependency injection.
Feel free to skip everything you already know. I can't detail everything here because your question is quite vague. So this answer will guide on how to find your solution or at least focus your question.
Symfony Allows you to connect multiple databases. You need to do a bt workout to achieve this.
Step#1
Add parameters to parameters.yml
file. The second database connection could be added using the following parameters:
Parameters:
# First database
database_host: 127.0.0.1
database_port: null
database_name: qmsfumgabd
database_user: qmsfumgabd
database_password: xxx9bxxMxx
# Second database
database2_host: 127.0.0.1
database2_port: null
database2_name: huscqxzwaw
database2_user: huscqxzwaw
database2_password: dxxxFXxxxB
Step#2
The next step is to get these credentials in the config.yml
:
doctrine:
# Configure the abstraction layer
dbal:
# Set the default connection to default
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
database2:
driver: pdo_mysql
host: '%database2_host%'
port: '%database2_port%'
dbname: '%database2_name%'
user: '%database2_user%'
password: '%database2_password%'
charset: UTF8
Step#3
Finally, specify the mapping of each bundle in the project:
# Configure the ORM
orm:
default_entity_manager: default
entity_managers:
# Register which bundle should use which connection
default:
connection: default
mappings:
AppBundle: ~
DemoBundle: ~
database2:
connection: database2
mappings:
CloudwaysBundle: ~
Step#4
Now for calling any of the entity manager use connection names simply:
class UserController extends Controller
{
public function indexAction()
{
// All 3 return the "default" entity manager
$em = $this->getDoctrine()->getManager();
$em = $this->getDoctrine()->getManager('default');
$em = $this->get('doctrine.orm.default_entity_manager');
// Both of these return the "database2" entity manager
$anotherEm = $this->getDoctrine()->getManager('database2');
$anotherEm = $this->get('doctrine.orm.database2_entity_manager');
}
}
You can also take help from symfony's documentation for multiple databases .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.