Symfony 4 EntityManager 多数据库
Symfony 4 EntityManager multiple database
我想在我的 APP\Repository 文件中使用多个数据库执行 SQL 请求。
我找到了这个,但它只适用于 APP\Controller 文件。
Symfony multiple Entity Managers and Connections
但无法在存储库文件中执行 $this->getDoctrine()->getManager('database1')
这是我的代码:
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
提前感谢您的回答,如果我不清楚,请随时提问
这是我实现的,可以提供帮助:
App\Repository
public function __construct(ManagerRegistry $mr)
{
$this->mr = $mr;
}
function Getdata() {
$em = $this->mr->getManager("db1");
$sql = "SELECT * FROM table"
$conn = $em->getConnection();
$query = $conn->prepare($sql);
$query->execute();
return ($query);
}
/config/doctrine.yaml
doctrine:
dbal:
default_connection: db1
connections:
db1:
url: '%env(DB1)%'
driver: pdo_mysql
server_version: 'mariadb-10.4.7'
db2:
url: '%env(DB2)%'
driver: pdo_mysql
server_version: 'mariadb-10.4.7'
orm:
auto_generate_proxy_classes: true
entity_managers:
db1:
connection: db1
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
db2:
connection: db2
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/'
prefix: 'App\Entity'
我想在我的 APP\Repository 文件中使用多个数据库执行 SQL 请求。
我找到了这个,但它只适用于 APP\Controller 文件。
Symfony multiple Entity Managers and Connections
但无法在存储库文件中执行 $this->getDoctrine()->getManager('database1')
这是我的代码:
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
提前感谢您的回答,如果我不清楚,请随时提问
这是我实现的,可以提供帮助:
App\Repository
public function __construct(ManagerRegistry $mr)
{
$this->mr = $mr;
}
function Getdata() {
$em = $this->mr->getManager("db1");
$sql = "SELECT * FROM table"
$conn = $em->getConnection();
$query = $conn->prepare($sql);
$query->execute();
return ($query);
}
/config/doctrine.yaml
doctrine:
dbal:
default_connection: db1
connections:
db1:
url: '%env(DB1)%'
driver: pdo_mysql
server_version: 'mariadb-10.4.7'
db2:
url: '%env(DB2)%'
driver: pdo_mysql
server_version: 'mariadb-10.4.7'
orm:
auto_generate_proxy_classes: true
entity_managers:
db1:
connection: db1
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
db2:
connection: db2
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/'
prefix: 'App\Entity'