0

I'm trying to create a new database and load the schema from the Controller.

I created the database on the fly, but don't know how I can select that new database to load the schema.

This is my code:

parameters.yml

parameters:

    # Admin database
    database_driver: pdo_mysql
    database_host: localhost
    database_port: '3306'
    database_name: app_db
    database_user: root
    database_password: XXX

    # A Center database
    database_driver2: pdo_mysql
    database_host2: localhost
    database_port2: '3306'
    database_name2: center_1
    database_user2: root
    database_password2: XXX

config.yml

# Doctrine Configuration
doctrine:
    dbal:
        default_connection:   default
        connections:

            default:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8

            center:
                driver:   %database_driver2%
                host:     %database_host2%
                port:     %database_port2%
                dbname:   %database_name2%
                user:     %database_user2%
                password: %database_password2%
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        #auto_mapping: true

        default_entity_manager: default
        entity_managers:

            default:
                connection: default
                mappings:
                    BackendBundle: ~
                    CenterBundle: ~
            center:
                connection: center
                mappings:
                    UsuarioBundle: ~

In the controller when i add a new center i create the database on the fly, center_1, center_2...

###################
# CREATE DATABASE #
###################

$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');

$connection = $connectionFactory->createConnection(array(
    'driver' => 'pdo_mysql',
    'user' => 'root',
    'password' => XXX,
    'host' => 'localhost',
    'dbname' => 'center_'.$center->getId(),
));

$params = $connection->getParams();
$name = isset($params['path']) ? $params['path'] : $params['dbname'];

unset($params['dbname']);

$tmpConnection = DriverManager::getConnection($params);

// Only quote if we don't have a path
if (!isset($params['path'])) {
    $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
}

$error = false;

try {
    $tmpConnection->getSchemaManager()->createDatabase($name);
    echo sprintf('<info>Created database for connection named <comment>%s</comment></info>', $name);
} catch (\Exception $e) {
    echo sprintf('<error>Could not create database for connection named <comment>%s</comment></error>', $name);
    echo sprintf('<error>%s</error>', $e->getMessage());
    $error = true;
}

$tmpConnection->close();

My question is, how i could select that database and load the schema.

Is posible change on the fly the parameters.yml for a Center database, and then execute de command:

app/console doctrine:schema:create --em=center

If is not posible, another idea of ​​how to do it?

********* EDIT 14/01/13

I add an example that i like, maybe I have not explained well.

When i add a new center, the above code create a new center, and a new database for this center, for example the database call "center_13"

Now if i like create the database schema i need go to parameters.yml and create a new parameters for this database

parameters.yml

parameters:

    ....

    # A Center database
    database_name_13: center_13

also i need create a new connection in config.yml

config.yml

# Doctrine Configuration
doctrine:
    dbal:
        default_connection:   default
        connections:

            default:
                ...

            center_13:
                driver:   %database_driver%
                host:     %database_host%
                port:     %database_port%
                dbname:   %database_name_13%
                user:     %database_user%
                password: %database_password%
                charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        #auto_mapping: true

        default_entity_manager: default
        entity_managers:

            default:

                ...

            center_13:
                connection: center_13
                mappings:
                    UsuarioBundle: ~

now if i run the command

app/console doctrine:schema:create --em=center_13

it configure the database "center_13" with the UsuarioBundle entities.

I'd like know how to do this process in de create center action of my controller

1 Answer 1

1

The commands

app/Console doctrine:schema:create

or

app/console doctrine:schema:update

will configure your database as defined in your model configuration, but not from a controller.

You could create a custom symfony command and associate it to your createDatabaseController but I would make sure it's worth the effort first.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, i understand you.. maybe I have not explained well, i edit the question and added a example. Thanks!
Is it only for testing purpose or do you want this behaviour in production ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.