I would like to execute SQL Request with multiple database inside my APP\Repository file.
I found this, but it's only work with APP\Controller file.
Symfony multiple Entity Managers and Connections
But unable to do $this->getDoctrine()->getManager('database1') inside a Repository file
Here is my code :
App\Repository
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @param string $code
* @return mixed[]
* @throws DBALException
*/
function Getdata(string $code) {
// I would be able to execute $sql with my DATABASE2
$sql = "SELECT * FROM api";
$conn = $this->em->getConnection();
$query = $conn->prepare($sql);
$query->execute();
$result = $query->fetchAll();
return($result);
}
/config/services.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(DATABASE1)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
# configure these for your database server
url: '%env(DATABASE2)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
Thanks by advance for the answer, if i'm not clear, don't hesitate to ask