原则动态创建一个实体 [英] doctrine create an entity dynamically

查看:86
本文介绍了原则动态创建一个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法在数据库动态(从php脚本)中创建和修改实体和表的表达?我想从数组中生成一个实体:

  fields {
id:integer,
name:字符串,
...等等
}

而不是生成bd中的表



我只知道一个简单的解决方案:创建yml或xml文件,并从我的脚本运行控制台命令,或者使用DBAL

解决方案


我只知道一个简单的解决方案:创建yml或xml文件,并从我的脚本运行控制台命令,或使用DBAL


您还可以从命令行生成一个实体,例如:

  $ php app / console generate:doctrine:entity --no-interaction \\ \\ 
--entity = AcmeBlogBu​​ndle:Post \
--fields =id:integer title:string(100)body:text\
--format = xml

这使用SensioGeneratorBundle,即 dev 和 test 环境



所以,这是一个有点黑客,但你需要调用这个命令与正确的环境:

  $ php app / console generate:doctrine:entity [...] --env = dev 
pre>

过程组件<可以使用a>来启动这个命令。



所以你最终可能会这样:

 使用Symfony\Component\Process\Process; 
使用Symfony\Component\Process\Exception\ProcessFailedException;

$ command ='php app / console generate:doctrine:entity --no-interaction'。
'--entity = AcmeBlogBu​​ndle: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 and test 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆