Joel Verhagen
Joel Verhagen

Reputation: 5282

Run Doctrine 2 CLI Tools Tasks from a script run from the browser

A little background information: I am working on integrating Doctrine into a CodeIgniter application. I have it working, but I would like to be able to run the Doctrine command line (CLI) tasks from the browser, i.e. not from the command line script.

The reason I desire this is because I will be running Doctrine and CodeIgniter on a shared hosting package where I will not have command line access.

This seems like a very basic feature, but is not readily available with Doctrine 2.

My last-ditch effort will be going into the command line tool and figuring out how the tasks are being executed then duplicating that code in a CodeIgniter controller.

If there is any simpler way to do this, please let me know.

Thanks!

Unanswered duplicate posted a while back.

Upvotes: 3

Views: 979

Answers (2)

Tom Anderson
Tom Anderson

Reputation: 66

For the following

$doctrine = \Zend_Registry::get('doctrine');
$em = $doctrine->getEntityManager();
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);

Get the SQL to update the current schema:

$sqlArray = $tool->getUpdateSchemaSql($em->getMetadataFactory()->getAllMetadata());

Update the schema with the current metadata

$res = $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());

Create the schema.

$res = $tool->createSchema($em->getMetadataFactory()->getAllMetadata());

This belongs in an install script. Just create and verify the db connection

$conn = $doctrine->getConnection();
$sql = "SELECT * FROM users";
try {
    $stmt = $conn->query($sql); // Simple (too simple?)
    die('Already installed');
} catch (Exception $e) {
    // Table not found, continue
}

Then create your schema.

Upvotes: 1

timdev
timdev

Reputation: 62914

You probably don't want to try to run the command-line tools without a command-line.

However, you can do it yourself in scripts pretty simply. For instance, if you wanted to do things that orm:schema-tool:* does, you'd start here

Upvotes: 0

Related Questions