如何从控制器以编程方式添加学说实体管理器?
How to add doctrine entity manager programmatically from controller?
我有一个控制器方法,我在其中以编程方式向 config.yml
添加了学说连接和学说实体管理器。
当我尝试获取刚刚通过 $doctrine->getManager('newManagerNameAdded')
添加的实体管理器时,Doctrine 告诉我该实体管理器不存在。
我认为有必要指定 doctrine 来重新加载它的配置,但我找不到这样做的方法。
有人做过吗?
在 config.yml
中将实体管理器添加到学说配置后,我尝试创建一个 cache:clear
,但没有成功。
另一方面,如果我执行两次我的控制器方法,第二次 doctrine 知道如何恢复第一次在配置中添加的实体管理器。
$configArray = Yaml::parseFile($this->configFilePath);
// Si la connection n'existe pas, on l'ajoute
if (!array_key_exists($entityManagerName, $configArray["doctrine"]["orm"]["entity_managers"])) {
$configArray["doctrine"]["orm"]["entity_managers"][$entityManagerName] = array(
"connection" => $entityManagerName,
"mappings" => array("AppBundle" => null),
);
}
$yamlUpdated = Yaml::dump($configArray);
file_put_contents($this->configFilePath, $yamlUpdated);
$this->doctrine->getManager($entityManagerName); // This throw an exception 'Doctrine entity manager named $connectionName does not exist'
这是 doctrine.yaml 文件:
doctrine:
dbal:
default_connection: core
connections:
core:
driver: pdo_pgsql
host: '%core_database_host%'
port: '%core_database_port%'
dbname: '%core_database_name%'
user: '%core_database_user%'
password: '%core_database_password%'
charset: UTF8
customer2:
driver: pdo_pgsql
host: '%demo_database_host%'
port: '%demo_database_port%'
dbname: '%demo_database_name%'
user: '%demo_database_user%'
password: '%demo_database_password%'
charset: UTF8
customer1:
driver: pdo_pgsql
host: '%waigeo_database_host%'
port: '%waigeo_database_port%'
dbname: '%waigeo_database_name%'
user: '%waigeo_database_user%'
password: '%waigeo_database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: core
entity_managers:
core:
connection: core
mappings:
CoreBundle: null
customer1:
connection: customer1
mappings:
AppBundle: null
dql:
datetime_functions:
date_format: DoctrineExtensions\Query\Postgresql\DateFormat
customer2:
connection: customer2
mappings:
AppBundle: null
dql:
datetime_functions:
date_format: DoctrineExtensions\Query\Postgresql\DateFormat
enter code here
我创建了解决方案。
我将新的连接和实体管理器添加到 doctrine.yml 配置文件,并在我的控制器操作中通过学说 API 创建了一个实体管理器。
这是解决我问题的代码。
// STEP 1: Update doctrine.yml for add connection and entity manager
$configArray = Yaml::parseFile($this->configFilePath.'doctrine.yml');
$configArray["doctrine"]["dbal"]["connections"][$connectionName] = array(
"driver" => "pdo_pgsql",
"host" => '127.0.0.1',
"port" => '5432',
"dbname" => $databaseName,
"user" => $databaseUsername,
"password" => $databaseUserPassword,
"charset" => "utf8"
);
$configArray["doctrine"]["orm"]["entity_managers"][$connectionName] = array(
"connection" => $connectionName,
"mappings" => array("AppBundle" => null),
"dql" => array("datetime_functions" => array("date_format" => "DoctrineExtensions\Query\Postgresql\DateFormat"))
);
$yamlUpdated = Yaml::dump($configArray);
file_put_contents($this->configFilePath.'doctrine.yml', $yamlUpdated);
// STEP 2 : Create new entity manager via Doctrine APIs
$config = Setup::createAnnotationMetadataConfiguration(array($this->getParameter('kernel.project_dir'). DIRECTORY_SEPARATOR ."/src/AppBundle/Entity"), false, null, null, false);
$em = EntityManager::create(array(
"driver" => "pdo_pgsql",
"host" => '127.0.0.1',
"port" => '5432',
"dbname" => $databaseName,
"user" => $databaseUsername,
"password" => $databaseUserPassword,
"charset" => "utf8"
), $config);
// STEP 3 : Create database schema
$metadata = $em->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($em);
$tool->createSchema($metadata);
// STEP 4 : Make what do you want with the new entitymanager
我希望这个答案能帮助到同样需要它的人
我有一个控制器方法,我在其中以编程方式向 config.yml
添加了学说连接和学说实体管理器。
当我尝试获取刚刚通过 $doctrine->getManager('newManagerNameAdded')
添加的实体管理器时,Doctrine 告诉我该实体管理器不存在。
我认为有必要指定 doctrine 来重新加载它的配置,但我找不到这样做的方法。
有人做过吗?
在 config.yml
中将实体管理器添加到学说配置后,我尝试创建一个 cache:clear
,但没有成功。
另一方面,如果我执行两次我的控制器方法,第二次 doctrine 知道如何恢复第一次在配置中添加的实体管理器。
$configArray = Yaml::parseFile($this->configFilePath);
// Si la connection n'existe pas, on l'ajoute
if (!array_key_exists($entityManagerName, $configArray["doctrine"]["orm"]["entity_managers"])) {
$configArray["doctrine"]["orm"]["entity_managers"][$entityManagerName] = array(
"connection" => $entityManagerName,
"mappings" => array("AppBundle" => null),
);
}
$yamlUpdated = Yaml::dump($configArray);
file_put_contents($this->configFilePath, $yamlUpdated);
$this->doctrine->getManager($entityManagerName); // This throw an exception 'Doctrine entity manager named $connectionName does not exist'
这是 doctrine.yaml 文件:
doctrine:
dbal:
default_connection: core
connections:
core:
driver: pdo_pgsql
host: '%core_database_host%'
port: '%core_database_port%'
dbname: '%core_database_name%'
user: '%core_database_user%'
password: '%core_database_password%'
charset: UTF8
customer2:
driver: pdo_pgsql
host: '%demo_database_host%'
port: '%demo_database_port%'
dbname: '%demo_database_name%'
user: '%demo_database_user%'
password: '%demo_database_password%'
charset: UTF8
customer1:
driver: pdo_pgsql
host: '%waigeo_database_host%'
port: '%waigeo_database_port%'
dbname: '%waigeo_database_name%'
user: '%waigeo_database_user%'
password: '%waigeo_database_password%'
charset: UTF8
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: core
entity_managers:
core:
connection: core
mappings:
CoreBundle: null
customer1:
connection: customer1
mappings:
AppBundle: null
dql:
datetime_functions:
date_format: DoctrineExtensions\Query\Postgresql\DateFormat
customer2:
connection: customer2
mappings:
AppBundle: null
dql:
datetime_functions:
date_format: DoctrineExtensions\Query\Postgresql\DateFormat
enter code here
我创建了解决方案。
我将新的连接和实体管理器添加到 doctrine.yml 配置文件,并在我的控制器操作中通过学说 API 创建了一个实体管理器。
这是解决我问题的代码。
// STEP 1: Update doctrine.yml for add connection and entity manager
$configArray = Yaml::parseFile($this->configFilePath.'doctrine.yml');
$configArray["doctrine"]["dbal"]["connections"][$connectionName] = array(
"driver" => "pdo_pgsql",
"host" => '127.0.0.1',
"port" => '5432',
"dbname" => $databaseName,
"user" => $databaseUsername,
"password" => $databaseUserPassword,
"charset" => "utf8"
);
$configArray["doctrine"]["orm"]["entity_managers"][$connectionName] = array(
"connection" => $connectionName,
"mappings" => array("AppBundle" => null),
"dql" => array("datetime_functions" => array("date_format" => "DoctrineExtensions\Query\Postgresql\DateFormat"))
);
$yamlUpdated = Yaml::dump($configArray);
file_put_contents($this->configFilePath.'doctrine.yml', $yamlUpdated);
// STEP 2 : Create new entity manager via Doctrine APIs
$config = Setup::createAnnotationMetadataConfiguration(array($this->getParameter('kernel.project_dir'). DIRECTORY_SEPARATOR ."/src/AppBundle/Entity"), false, null, null, false);
$em = EntityManager::create(array(
"driver" => "pdo_pgsql",
"host" => '127.0.0.1',
"port" => '5432',
"dbname" => $databaseName,
"user" => $databaseUsername,
"password" => $databaseUserPassword,
"charset" => "utf8"
), $config);
// STEP 3 : Create database schema
$metadata = $em->getMetadataFactory()->getAllMetadata();
$tool = new SchemaTool($em);
$tool->createSchema($metadata);
// STEP 4 : Make what do you want with the new entitymanager
我希望这个答案能帮助到同样需要它的人