Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DoctrineUpdateTask

  • Loading branch information...
commit c9765c23c2e361dc3d030c345a8e5d2a2e076205 1 parent 5e78d8a
heartsentwined authored
View
2  src/task/DoctrineProxyTask.php
@@ -1,8 +1,6 @@
<?php
require_once __DIR__ . '/../../../../autoload.php';
-use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
-use Doctrine\ORM\Tools\EntityRepositoryGenerator;
use Symfony\Component\Yaml\Yaml;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Application;
View
1  src/task/DoctrineRepoTask.php
@@ -1,7 +1,6 @@
<?php
require_once __DIR__ . '/../../../../autoload.php';
-use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
use Symfony\Component\Yaml\Yaml;
use Zend\ServiceManager\ServiceManager;
View
105 src/task/DoctrineUpdateTask.php
@@ -0,0 +1,105 @@
+<?php
+require_once __DIR__ . '/../../../../autoload.php';
+
+use Doctrine\ORM\Tools\SchemaTool;
+use Symfony\Component\Yaml\Yaml;
+use Zend\ServiceManager\ServiceManager;
+use Zend\Mvc\Application;
+
+class DoctrineUpdateTask extends Task
+{
+ protected $failonerror;
+
+ /**
+ * output directory for entity classes
+ *
+ * @param string $output
+ * @return void
+ */
+ public function setOutput($output)
+ {
+ if (!is_dir($output)) {
+ throw new BuildException(sprintf(
+ 'Output directory does not exist: %s',
+ realpath($output)
+ ));
+ }
+ if (!is_writable($output)) {
+ throw new BuildException(sprintf(
+ 'Output directory is not writable: %s',
+ realpath($output)
+ ));
+ }
+ $this->output = realpath($output);
+ }
+
+ /**
+ * if error occured, whether build should fail
+ *
+ * @param bool $value
+ * @return void
+ */
+ public function setFailonerror($value)
+ {
+ $this->failonerror = $value;
+ }
+
+ /**
+ * init
+ *
+ * @return void
+ */
+ public function init()
+ {
+ }
+
+ /**
+ * main method
+ *
+ * @return void
+ */
+ public function main()
+ {
+ static $em;
+
+ if ($em === null) {
+ $wd = getcwd();
+
+ $previousDir = '.';
+ while (!file_exists('config/application.config.yml')) {
+ $dir = dirname(getcwd());
+
+ if ($previousDir === $dir) {
+ throw new BuildException('Unable to locate "config/application.config.yml"');
+ }
+
+ $previousDir = $dir;
+ chdir($dir);
+ }
+
+ $application = Application::init(Yaml::parse('config/application.config.yml'));
+ $em = $application->getServiceManager()->get('doctrine.entitymanager.orm_default');
+
+ chdir($wd);
+
+ /* finish bootstrapping zf2 */
+ }
+
+ $metadatas = $em->getMetadataFactory()->getAllMetadata();
+
+ if (!empty($metadatas)) {
+ $tool = new SchemaTool($em);
+ $sqls = $tool->getUpdateSchemaSql($metadatas, false);
+
+ if (0 === count($sqls)) {
+ $this->log('Nothing to update - your database is already in sync with the current entity metadata.');
+ } else {
+ $this->log('Updating database schema...');
+ $tool->updateSchema($metadatas, false);
+ $this->log(sprintf('Database schema updated successfully! %s queries were executed', count($sqls)));
+ }
+ } else {
+ $this->log('No metadata classes to process');
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.