简体   繁体   中英

How to save doctrine database schema in a Symfony controller?

I'm using Symfony 2.3 and Doctrine 2 and i need that an user save the schema of a Doctrine Database to a file (*.sql). I need it into an action method and then send the file to the user

You need to execute following command:

.app/console doctrine:schema:create --dump-sql >schema.sql

And here's the answer how to run Command from Controller : How can I run symfony 2 run command from controller

Just to get you started, this should work in concept. I didn't get to run it, so I assume it might need a little tweaks from your side.

<?php
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;

// your controller

public function myAction()
{
    $command = new CreateSchemaDoctrineCommand();
    $command->setContainer($this->container);
    $input = new ArrayInput(array('--dump-sql' => true));
    $output = new NullOutput();
    $schema = $command->run($input, $output); //This is your schema

    // Write it to a file if you want
    file_put_contents('path/to/schema.sql', $schema);
}

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM