我想用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:[email protected]: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。
通过运行现在的代码,我得到这2个错误。
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."
通过在互联网上寻找解决方案,我知道我必须重新初始化容器,但我没有找到一种方法来解决我的特殊情况,我希望有其他的方法来实现Symfony4和Doctrine的工作。