0

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

Oscar Acevedo
  • 1,144
  • 1
  • 12
  • 19

2 Answers2

1

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

Community
  • 1
  • 1
Bartek
  • 1,349
  • 7
  • 13
1

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:

Community
  • 1
  • 1
Ramy Nasr
  • 2,367
  • 20
  • 24