有没有办法在数据库中动态创建和修改实体和表(从 php 脚本)?
例如,我想从一个数组中生成一个实体:
fields{
id: integer,
name: string,
... and so on
}
然后在 bd 中生成一个表
我只知道一个简单的解决方案:创建 yml 或 xml 文件并从我的脚本运行控制台命令,或者使用 DBAL
最佳答案
I know only one simple solution: to create yml or xml file and run a console command from my script, or use DBAL
您也可以generate an entity from command line ,例如:
$ php app/console generate:doctrine:entity --no-interaction \
--entity=AcmeBlogBundle:Post \
--fields="id:integer title:string(100) body:text" \
--format=xml
这使用了 SensioGeneratorBundle,即 defined only in dev
and test
environments .
所以,它有点 hacky,但您需要在正确的环境中调用此命令:
$ php app/console generate:doctrine:entity […] --env=dev
Process component可用于启动此命令。
所以你可能会得到这样的结果:
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();
关于doctrine-orm - 学说动态创建实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37779492/