0

I am new to ORM world and integrating doctrine 2 with zf project. I have gone through the doctrine documentation and various articles and integrated it :).

I have an existing DB from which I got to generate my mappers and entities. I am willing to use XML/YAML mappers instead of default annotations.

All I see everywhere is CLI commands to generate the mappers and entities, by executing the below commands in the bin directory where the "doctrine.php(provided in doctrine official docs)" file exists.

To generate Mappers:

         *php doctrine orm:convert-mapping --from-database xml  /path/to/mappers*

To generate Entities/Model Classes:

         *./doctrine orm:generate-entities /path/to/models/or/entities*

But I dont want to execute the commands and generate the mappers/entities. I am looking for a PHP script which does it for me(by calling an action method in a controller Ex: www.doctrineproj.com/admin/mdoels/autogenerate/).

Do we have any API class or any way to do this by php code, instead of executing the CLI script. may be by calling like

To generate mappers: doctrine_core:: generateMappers(dbParams,mapperDriver)

To generate entities: doctrine_core::generateEntities(metaData)

OR

is there any way to execute all CLI commands from the php script file using the system like calls?

I tried using exec() and system() by using the above cli commands. But it isn't working well :(.

Here is the code which I used to generate the mappers from my action method

public function autogenerateAction(){

     $result = array();
    //change the current working DIR to bin
    $cliPath = "cd ". APPLICATION_PATH . "/bin";
    exec($cliPath,$result);

    //construct the CMD to generate the XML files from the DB tables
    $mapperDir = APPLICATION_PATH. "/models/entities/xml-mapper/";
    $mapperType = "xml";

    $generateMappersCmd = "php doctrine orm:convert-mapping --from-database  " . $mapperType . ' '  . $mapperDir ;

    //execute the CMD
    $result = system($generateMappersCmd);

}

but the above code is not creating the mappers at the destined directory.

Please suggest me a best solution to auto generate the mappers and entities from the DB using automated php script.

Looking forward a best solution.

Thanks Raj

Raj
  • 91
  • 2
  • 3
  • 9

1 Answers1

0

Try next (in some method of controller):

$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);

$options = array(
    'command' => 'doctrine:generate:entity',
    '--entity' => "SomeDemoBundle:YourEntityName",
    '--fields' => "name:string(255) price:float description:text",
    '--with-repository' => true,
    '--format' => 'xml',
);

$fp = tmpfile();
$out = new StreamOutput($fp);
$in = new \Symfony\Component\Console\Input\ArrayInput($options);
$in->setInteractive(false);
$application->run($in, $out);
Ivan
  • 2,316
  • 2
  • 24
  • 22