Doctrine 2 CLI commands from within PHP
Hey all. I am writing a program that will transform some data in our database, and then call Doctrine to build YAML files from said Mysql Database structure. I have Doctrine working from within PHP. However I can't figure out how to call the CLI commands from within PHP. Following is the Doctrine 2 CLI command that does what I need.
php ./doct开发者_StackOverflow中文版rine orm:convert-mapping --filter="users" --from-database yml ./test
This command works from the Linux command line, but how to I do this same thing via Doctrine objects? I don't want to just use the PHP exec statement to send a command to the shell. I wish to use the Doctrine object model.
Don!:
Apparently this is not a very common programming method. However, I have used Doctrine from PHP by calling it via the PHP EXEC command. I know you said that you would not like to do it this way. However, it actually works quite well. Below is an example of such a solution.
$cmd_string = "php ./doctrine orm:generate-entities --generate-annotations=1 --regenerate-entities=1 $this->entity_file_dir";
$result = array();
exec($cmd_string, &$result);
Hope this helps, -Don!
I stumbled upon this question when trying to execute a command directly from a PHP script, without using the CLI.
Particularly, I was needing to call orm:ensure-production-settings. Each Doctrine command has its own class: http://www.doctrine-project.org/api/orm/2.4/namespace-Doctrine.ORM.Tools.Console.Command.html
I solved it the following way:
$entityManager = ...; // Get the entity manager somehow in your application
// Creates the helper set
$helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
// Initializes the desired command and sets the helper set
// In your case it should be ConvertMappingCommand instead
$command = new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand();
$command->setHelperSet($helperSet);
// Initializes the input
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Input.html
$input = new \Symfony\Component\Console\Input\ArgvInput(); // Input coming from the CLI arguments
// Initializes the output
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Output.html
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); // Prints the output in the console
// Runs the command
$command->run($input, $output);
I'm new to Doctrine so I'm not exactly sure how this works, but it does. Any comment is appreciated.
精彩评论