原则动态创建一个实体 [英] doctrine create an entity dynamically
问题描述
有没有办法在数据库动态(从php脚本)中创建和修改实体和表的表达?我想从数组中生成一个实体:
fields {
id:integer,
name:字符串,
...等等
}
而不是生成bd中的表
我只知道一个简单的解决方案:创建yml或xml文件,并从我的脚本运行控制台命令,或者使用DBAL
我只知道一个简单的解决方案:创建yml或xml文件,并从我的脚本运行控制台命令,或使用DBAL
您还可以从命令行生成一个实体,例如:
$ php app / console generate:doctrine:entity --no-interaction \\ \\
--entity = AcmeBlogBundle:Post \
--fields =id:integer title:string(100)body:text\
--format = xml
这使用SensioGeneratorBundle,即 dev 和 test $ c $中定义的标准/ blob / f5b1ae88c3d2338f43a18363811f03bae196ff54 / app / AppKernel.php#L25rel =nofollow c>环境
。
所以,这是一个有点黑客,但你需要调用这个命令与正确的环境:
$ php app / console generate:doctrine:entity [...] --env = dev
pre>
所以你最终可能会这样:
使用Symfony\Component\Process\Process;
使用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();
//执行命令完成
如果(!$ process-> isSuccessful()){
抛出新的ProcessFailedException($ process);
}
echo $ process-> getOutput();
Is there a way to create and modify entities and tables in a db dynamically (from a php script)?
For example, I want to generate an entity from an array:
fields{ id: integer, name: string, ... and so on }
And than generate a table in the bd
I know only one simple solution: to create yml or xml file and run a console command from my script, or use DBAL
解决方案I know only one simple solution: to create yml or xml file and run a console command from my script, or use DBAL
You can also generate an entity from command line, for example:
$ php app/console generate:doctrine:entity --no-interaction \ --entity=AcmeBlogBundle:Post \ --fields="id:integer title:string(100) body:text" \ --format=xml
This uses the SensioGeneratorBundle, that is defined only in
dev
andtest
environments.So, it is a little bit hacky but you'll need to call this command with the right environment:
$ php app/console generate:doctrine:entity […] --env=dev
The Process component can be used in order to launching this command.
So you may end up with something like this:
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();
这篇关于原则动态创建一个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!