Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine create an entity dynamically

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

like image 966
Alex M Avatar asked Mar 06 '26 11:03

Alex M


1 Answers

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 launch 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();
like image 128
A.L Avatar answered Mar 09 '26 16:03

A.L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!