有没有办法创建和modify动态数据库中的实体和表(来自 php 脚本)?
例如,我想从数组生成一个实体:
fields{
id: integer,
name: string,
... and so on
}
然后在bd中生成一个表
我只知道一种简单的解决方案:创建 yml 或 xml 文件并从我的脚本运行控制台命令,或使用 DBAL
我只知道一种简单的解决方案:创建 yml 或 xml 文件并从我的脚本运行控制台命令,或使用 DBAL
你也可以从命令行生成实体 http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_entity.html, 例如:
$ php app/console generate:doctrine:entity --no-interaction \
--entity=AcmeBlogBundle:Post \
--fields="id:integer title:string(100) body:text" \
--format=xml
这里使用SensioGeneratorBundle,即仅定义于dev and test环境 https://github.com/symfony/symfony-standard/blob/f5b1ae88c3d2338f43a18363811f03bae196ff54/app/AppKernel.php#L25.
因此,这有点棘手,但您需要在正确的环境下调用此命令:
$ php app/console generate:doctrine:entity […] --env=dev
The 流程组件 http://symfony.com/doc/current/components/process.html可以用来启动此命令。
所以你最终可能会得到这样的结果:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$command = 'php app/console generate:doctrine:entity --no-interaction '.
'--entity=AcmeBlogBundle:Post '.
'--fields="id:integer title:string(100) body:text" '.
'--format=xml';
$process = new Process($command);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)