动态创建数据库 [英] Creating the database on fly

查看:52
本文介绍了动态创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发laravel 4应用程序,因为我想允许用户注册该应用程序,当他们注册该应用程序时,应该允许注册用户使用一个新的空数据库,我正在使用mysql作为后端,我知道以下代码设置数据库连接

I am working on laravel 4 app, In that i wan to allow the user to register for the app and when they register the app should allow a new empty database to the registered user,I am using mysql for backend I know the following code to set the database connection

Config::set('database.connections', ConnectionArray);

并将其设置为默认值

Config::set('database.default', ConnectionKey); 

要手动连接另一个数据库,我可以使用这种方式

To connect another database manually i can use this way

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database', 
    'username'  => 'root',
    'password'  => '123456',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),
'mysql_tenant1' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'tenant1',  
    'username'  => 'root',
    'password'  => '123456',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

但是我希望应用程序可以动态创建数据库...

But i want the app should create database dynamically...

推荐答案

创建数据库本身将相对简单.复杂性在于以某种方式将这些数据库与用户相关联,然后提取这些数据库名称并在连接中使用它们.这应该有帮助:

Creating the Database itself will be relatively simple. The complexity comes in associating those databases to users somehow, and then pulling those database names and using them in connections. This should help:

在总体策略方面,请考虑创建一个具有以下功能的新存储库:

In terms of overall strategy, consider creating a new repository with a few functions:

  1. RegisterUser()-使用用户名/密码注册用户.
  2. CreateDatabase($ name,$ user_id)-这应该创建您的数据库并将其名称存储在表中,然后将其分配给您在注册用户"中创建的用户ID.
  1. RegisterUser() - This registers the user with username/password.
  2. CreateDatabase($name , $user_id) - This should create your database and store it's name in a table, and then assign it to the User ID you created in Register User.

您可以使用 DB :: statement()在Laravel中运行原始SQL查询(即-``DB :: statement('create database'.$ name);`,因此请坚持在您的CreateDatabase函数.我在本地进行了测试,效果很好.

You can run raw SQL queries in Laravel with DB::statement() (i.e - ``DB::statement('create database' . $name);`, so stick that in your CreateDatabase function. I tested this locally and it works just fine.

注意:您需要进行一些验证,以确保数据库不存在,或者您的用户(显然)将得到一个错误.

NOTE: You'll want to do some validation to make sure the database doesn't already exist, or your user is going to (obviously) get an error.

然后,您可以按用户ID从表中提取数据库名称,并按照下面的SO中的描述与其建立连接!

Then, you can pull the Database name from the table by User ID and create connections with it as described in the below SO!

Laravel 4:多个租户应​​用程序,每个租户是其自己的数据库和一个全局数据库

一件事-当您更改数据库结构时,您将不得不分别更新每个数据库.请注意,Artisan允许使用-database ="database_name" 选项.

One thing - when you make a change to your database structure, you're going to have to update each database individually. Note that Artisan allows for this with the --database="database_name" option.

这篇关于动态创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆