I'm here to ask a question.
So, let me explain, I have a project of an monitoring application under symfony 5.
Currently I've done most of the work working with a single database containing logs from another application. What I'm looking to do now is to be able to connect dynamically to a database (MSSQL).
For the moment the connection to the database is done via the file "doctrine.yaml".
Doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_sqlsrv
host: 192.168.1.33
port: null
dbname: 'job'
user: 'sa'
password: 'Lasernet@2020'
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: 'AppEntityMain'
alias: default
I did this for the dynamic connection
DynamicConnection.php
<?php
namespace AppDoctrine;
class DynamicConnection {
public function __construct($dbname,$user,$password,$host,$driver,$port)
{
$this->dbname = $dbname;
$this->user = $user;
$this->password = $password;
$this->host = $host;
$this->driver = $driver;
$this->port = $port;
}
public function changeDatabase(){
$connectionParams = array(
'dbname' => $this->dbname,
'user' => $this->user,
'password' => $this->password,
'host' => $this->host,
'driver' => $this->driver,
'port' => $this->port
);
$conn = DoctrineDBALDriverManager::getConnection($connectionParams);
if($conn){
return $conn;
}else{
return "no";
}
}
public function getParams()
{
$connectionParams = array(
'driver' => $this->driver,
'host' => $this->host,
'port' => $this->port,
'dbname' => $this->dbname,
'user' => $this->user,
'password' => $this->password,
'charset' => "utf8mb4",
'driverOptions' => [],
'defaultTableOptions' => []
);
return $connectionParams;
}
}
And in my Controller
/**
* @Route("/testconnection", name="test_connect")
*/
public function testConnection(){
$dbname = "job";
$user = "sa";
$password = "Lasernet@2020";
$host = "192.168.1.34";
$driver = "pdo_sqlsrv";
$port = null;
$connection = new DynamicConnection($dbname,$user,$password,$host,$driver,$port);
$params = $connection->getParams();
$newEm = EntityManager::create($params,$this->em->getConfiguration(), $this->em->getEventManager());
$job = $newEm->getRepository(Job::class)->findAll();
dd($job);
}
The problem is that the findAll() returns all the records of the database "192.168.1.33" not the one of "192.168.1.34" which behaves a different number of records.
Is there another way to connect dynamically to the database or to modify the "doctrine.yaml" file directly using JS for example, but I don't think this is the best solution.
If someone has a solution to my problem to make my findAll() return the info from the 192 database
.168.1.34
Small precision the two databases have the same structure of tables, fields, etc.
Resolved :
Karol Dabrowski, I did what you did and it's working great, thank you very much!
question from:
https://stackoverflow.com/questions/65902878/symfony-5-dynamic-connection