我正在尝试使用Symfony 4自动创建数据库。以一种形式为我的数据库定义名称就足够了,然后我的控制器(或服务)自动使用该名称创建数据库,并使用已经创建的实体。
我正在使用Symfony 4.2和Mysql 5.7
我的doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
url: '%env(resolve:DATABASE_URL)%default'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
customer1:
url: '%env(resolve:DATABASE_URL)%customer1'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
customer2:
url: '%env(resolve:DATABASE_URL)%customer2'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
orm:
auto_generate_proxy_classes: true
default_entity_manager: gomyclic
entity_managers:
default:
connection: default
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer1:
connection: customer1
mappings:
Customer1:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Customer1
customer2:
connection: customer2
mappings:
Customer2:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Customer2
.env中的DATABASE_URL变量
DATABASE_URL=mysql://dbuser:dbsecret@127.0.0.1:3306/
还有我的控制器中的实际代码 修改doctrine.yaml文件的部分
use Symfony\Component\Yaml\Yaml;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
$path = __DIR__ . '/../../config/packages/doctrine.yaml';
$database_name = 'mydatabase';
$file = Yaml::parseFile($path);
$dbal = [
"url" => sprintf("%%env(resolve:DATABASE_URL)%%%s", strtolower($database_name)),
"driver" => "pdo_mysql",
"server_version" => "5.7",
"charset" => "utf8mb4",
"default_table_options" => [
"charset" => "utf8mb4",
"collate" => "utf8mb4_unicode_ci"
]
];
$orm = [
"connection" => strtolower($database_name),
"mappings" => [
ucfirst(strtolower($database_name)) => [
"is_bundle" => false,
"type" => "annotation",
"dir" => "%kernel.project_dir%/src/Entity",
"prefix" => "App\Entity",
"alias" => ucfirst(strtolower($database_name)),
]
]
];
$file['doctrine']['dbal']['connections'][strtolower($database_name)] = $dbal;
$file['doctrine']['orm']['entity_managers'][strtolower($database_name)] = $orm;
$yaml = Yaml::dump($file, 8);
file_put_contents($path, $yaml);
还有应该创建数据库并使用表来实现的部分
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'doctrine:database:create',
'--connection' => strtolower($database_name)
]);
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'doctrine:schema:create',
'--em' => strtolower($database_name)
]);
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
这样一来,我们就可以在同一个symfony应用程序上创建多个数据库,同时可以创建新数据库,而不必强迫手动编辑doctrine.yaml文件来创建新的dbal和orm。
通过现在运行代码,我得到了这两个错误:
Error thrown while running command
"doctrine:database:create --connection=mydatabase"
. Message:
"Doctrine ORM Connection named "mydatabase" does not exist."
Error thrown while running command
"doctrine:schema:create --em=mydatabase"
. Message:
"Doctrine ORM Manager named "mydatabase" does not exist."
通过在Internet上寻找解决方案,我知道我必须重新初始化容器,但是我没有找到一种方法来处理我的特殊情况,我希望有另一种方法可以使用Symfony4和Doctrine来完成此工作。
答案 0 :(得分:0)
您已创建3个数据库连接:
并且错误消息显示“ mydatabase”不存在。正确,未配置。 doctrine:database:create在doctrine.yaml中寻找配置。
因此,您将必须动态更改配置,这是一个巨大的安全漏洞。
您的用例看起来很适合docker设置。使用docker,您可以创建具有所需功能的基础映像,然后为新数据库创建新的dockerfile,然后启动新的容器。
对于每个容器中的启动脚本,可以有一个sql脚本,该脚本可以根据需要通过doctrine migration初始化所有表。