0

我是 ORM 世界的新手,并将学说 2 与 zf 项目集成。我已经阅读了学说文档和各种文章并将其集成:)。

我有一个现有的数据库,我可以从中生成我的映射器和实体。我愿意使用 XML/YAML 映射器而不是默认注释。

我在各处看到的都是 CLI 命令来生成映射器和实体,方法是在存在“教义.php(在教义官方文档中提供)”文件的 bin 目录中执行以下命令。

要生成映射器:

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

生成实体/模型类:

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

但我不想执行命令并生成映射器/实体。我正在寻找一个为我执行此操作的 PHP 脚本(通过在控制器中调用操作方法,例如:www.doctrineproj.com/admin/mdoels/autogenerate/)。

我们是否有任何 API 类或通过 php 代码执行此操作的任何方式,而不是执行 CLI 脚本。可能是通过调用

生成映射器:教义核心::generateMappers(dbParams,mapperDriver)

生成实体:教义核心::generateEntities(metaData)

或者

有没有办法使用系统调用从 php 脚本文件中执行所有 CLI 命令?

我尝试使用上述 cli 命令来使用 exec() 和 system() 。但是效果不好:(。

这是我用来从我的操作方法生成映射器的代码

公共函数自动生成操作(){

     $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);

}

但是上面的代码没有在目标目录中创建映射器。

请建议我使用自动 php 脚本从数据库中自动生成映射器和实体的最佳解决方案。

期待最佳解决方案。

谢谢拉吉

4

1 回答 1

0

尝试下一步(在某些控制器方法中):

$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);
于 2012-08-31T21:09:52.987 回答