/** * Updates DB Schema. * @throws \Exception if there are no changes in entities */ protected function updateDbSchema() { /** * @var $em \Doctrine\ORM\EntityManager */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $event = $em->getEventManager(); $sm = $em->getConnection()->getSchemaManager(); $allMetadata = $em->getMetadataFactory()->getAllMetadata(); $schemaTool = new SchemaTool($em); $entitiesMetadata = array($em->getClassMetadata(ApiUser::getClassName()), $em->getClassMetadata(DiamanteUser::getClassName())); $event->disableListeners(); $currentSchema = $sm->createSchema(); $schemaFromMetadata = $schemaTool->getSchemaFromMetadata($allMetadata); $entitiesSchema = $schemaTool->getSchemaFromMetadata($entitiesMetadata); $entitiesTables = $entitiesSchema->getTables(); $entitiesTableName = array_keys($entitiesTables); $currentDiamanteSchema = $this->getTargetSchema($currentSchema, $entitiesTableName); $diamanteSchemaFromMetadata = $this->getTargetSchema($schemaFromMetadata, $entitiesTableName); $comparator = new Comparator(); $diff = $comparator->compare($currentDiamanteSchema, $diamanteSchemaFromMetadata); $toUpdate = $diff->toSql($em->getConnection()->getDatabasePlatform()); if (empty($toUpdate)) { throw new \Exception('No new updates found. Diamante Api Bundle is up to date!'); } $conn = $em->getConnection(); foreach ($toUpdate as $sql) { $conn->executeQuery($sql); } }