/** * @see \PHPUnit_Framework_TestCase::tearDown() */ protected function tearDown() { $this->entityManager->getConnection()->rollback(); // Shutdown the kernel. $this->kernel->shutdown(); parent::tearDown(); }
public function getConnection() { // Retrieve PDO instance $pdo = $this->em->getConnection()->getWrappedConnection(); // Clear Doctrine to be safe $this->em->clear(); // Schema Tool to process our entities $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em); $classes = $this->em->getMetaDataFactory()->getAllMetaData(); // Drop all classes and re-build them for each test case $tool->dropSchema($classes); $tool->createSchema($classes); // Pass to PHPUnit return $this->createDefaultDBConnection($pdo, 'db_name'); }
public function flush($remoteOperations = null, $fetchMode = null) { // If $remoteOperations is null, flush() has been called from a server-side method so we don't want to clear the em if ($remoteOperations) { $this->em->clear(); } // Start the transaction $this->em->getConnection()->beginTransaction(); try { // Perform the flush $flushExecutor = new FlushExecutor($this->em, $remoteOperations, $this->deserializationWalker); $changeSets = $flushExecutor->flush(); // Go through the elements in the change sets making the entities Flextrine ready (apart from temporaryUidMap and entityDeletionIdMap which are not entities) if ($fetchMode) { $this->setFetchMode($fetchMode); } foreach ($changeSets as $changeSetType => $changeSet) { if ($changeSetType != "temporaryUidMap" && $changeSetType != "entityDeletionIdMap") { foreach ($changeSet as $oid => $entity) { $changeSet[$oid] = $this->flextrinize($entity); } } } // Commit the transaction $this->em->getConnection()->commit(); // Return the change sets so they can be replicated in Flextrine return $changeSets; } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->em->close(); throw $e; } }
public function onFlush(OnFlushEventArgs $eventArgs) { $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $this->revisionId = null; // reset revision foreach ($this->uow->getScheduledEntityDeletions() as $entity) { $class = $this->em->getClassMetadata(get_class($entity)); if (!$this->metadataFactory->isAudited($class->name)) { continue; } $entityData = array_merge($this->getOriginalEntityData($entity), $this->uow->getEntityIdentifier($entity)); $this->saveRevisionEntityData($class, $entityData, 'DEL'); } }
private function _fazerCadastroVoluntarioAux(\Entity\Voluntario $voluntario, $curso_id) { $curso = $this->curso_dao->find_one_by($curso_id); $voluntario->setCurso($curso); $voluntario->setStatus(STATUS_USUARIO_PENDENTE); $voluntario->setPresenca(false); $this->voluntario_dao->insert($voluntario); $this->em->getConnection()->commit(); }
/** * Ob onFlush dogodku dobimo vse spremembe polj in kolekcij ter * jih zapišemo v revizije * * @param \Doctrine\ORM\Event\OnFlushEventArgs $eventArgs */ public function onFlush(OnFlushEventArgs $eventArgs) { $this->em = $eventArgs->getEntityManager(); $this->conn = $this->em->getConnection(); $this->uow = $this->em->getUnitOfWork(); $this->platform = $this->conn->getDatabasePlatform(); $changes = $this->getChanges(); $this->saveChanges($changes); }
public function truncateTable($tableNames) { $tableNames = (array) $tableNames; $this->em->getConnection()->executeQuery('set foreign_key_checks = 0'); foreach ($tableNames as $table) { $this->em->getConnection()->executeQuery('TRUNCATE `' . $table . '`'); } $this->em->getConnection()->executeQuery('set foreign_key_checks = 1'); }
/** * Update schema if needed * * @param integer $articleNumber * * @return void */ private function updateSchema($articleNumber) { try { $this->orm->getRepository('Newscoop\\Image\\ArticleImage')->findOneBy(array('articleNumber' => (int) $articleNumber)); } catch (\Exception $e) { if ($e->getCode() === '42S22') { $this->orm->getConnection()->exec('ALTER TABLE ArticleImages ADD is_default INT(1) DEFAULT NULL'); } } }
private function fazerCadastroAutorAux(Entity\Autor $autor, $curso_id) { $this->autor_dao->insert($autor); $curso = $this->curso_dao->find_one_by($curso_id); $autor_curso = new \Entity\AutorCurso(); $autor_curso->setAutor($autor); $autor_curso->setCurso($curso); $this->autor_curso_dao->insert($autor_curso); $this->em->getConnection()->commit(); }
private function fazerCadastroOrientadorAux(Entity\Orientador $orientador, $campus_id) { $orientador->setStatus(STATUS_USUARIO_PENDENTE); $this->orientador_dao->insert($orientador); $campus = $this->campus_dao->find_one_by($campus_id); $orientador_campus = new \Entity\OrientadorCampus(); $orientador_campus->setOrientador($orientador); $orientador_campus->setCampus($campus); $this->orientador_campus_dao->insert($orientador_campus); $this->em->getConnection()->commit(); }
private function _fazerCadastroOuvinteAux(\Entity\Ouvinte $ouvinte, $instituicao_id, $campus_id, $curso_id) { $instituicao = $this->instituicao_dao->find_one_by($instituicao_id); $campus = $this->campus_dao->find_one_by($campus_id); $curso = $this->curso_dao->find_one_by($curso_id); $ouvinte->setInstituicao($instituicao); $ouvinte->setCampus($campus); $ouvinte->setCurso($curso); $ouvinte->setStatus(STATUS_USUARIO_PENDENTE); $this->ouvinte_dao->insert($ouvinte); $this->em->getConnection()->commit(); }
/** * Setup before class method * * @return void */ public static function setUpBeforeClass() { $app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); $app->bootstrap('doctrine'); self::$entityManager = $app->getBootstrap()->getResource('doctrine'); self::$metadata = array(); // Drop existing test schema self::dropSchema(self::$entityManager->getConnection()->getParams()); // Create test schema $tool = new SchemaTool(self::$entityManager); $tool->createSchema(self::getClassMetas(APPLICATION_PATH . '/../tests/library/Rexmac/Zyndax/Doctrine/Entity', 'Rexmac\\Zyndax\\Doctrine\\Entity\\')); }
private function fazerCadastroAvaliadorAux(\Entity\Avaliador $avaliador, $campus_id, $area_id) { $campus = $this->campus_dao->find_one_by($campus_id); $avaliador->setCampus($campus); $avaliador->setStatus(STATUS_USUARIO_PENDENTE); $this->avaliador_dao->insert($avaliador); $area = $this->area_tematica_dao->findOneById($area_id); $avaliadorArea = new \Entity\AvaliadorArea(); $avaliadorArea->setArea($area); $avaliadorArea->setAvaliador($avaliador); $this->avaliador_area_dao->insert($avaliadorArea); $this->em->getConnection()->commit(); }
/** * Prepare DB for tests by loading in predefined test data * * @return void */ private static function prepareDb() { $app = new \Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); $app->getBootstrap()->bootstrap('doctrine'); $app->getBootstrap()->bootstrap('registry'); self::$entityManager = $app->getBootstrap()->getResource('doctrine'); self::$metadata = array(); // Drop existing schema $params = self::$entityManager->getConnection()->getParams(); if (file_exists($params['path'])) { unlink($params['path']); } // Add BLOB data type mapping self::$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'gzblob'); // Add IP data type mapping self::$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('IP', 'ip'); // Create schema $tool = new SchemaTool(self::$entityManager); $metas = array_merge(self::getClassMetas(APPLICATION_PATH . '/../library/Rexmac/Zyndax/Entity', 'Rexmac\\Zyndax\\Entity\\'), self::getClassMetas(APPLICATION_PATH . '/../library/Rexmac/Zyndax/Monitor/Entity', 'Rexmac\\Zyndax\\Monitor\\Entity\\')); $tool->createSchema($metas); }
/** * Realiza envio de email para fases antigas */ public function execute() { foreach ($this->getUsuariosExterno() as $value) { $sqUsuario = $value['sqUsuarioExterno']; # excluir dados complementares $sql = "DELETE FROM sicae.usuario_externo_dado_complementar u WHERE u.sq_usuario_externo = {$sqUsuario}"; $this->_em->getConnection()->exec($sql); # excluir pessoa fisica $sql = "DELETE FROM sicae.usuario_pessoa_fisica u WHERE u.sq_usuario_externo = {$sqUsuario}"; $this->_em->getConnection()->exec($sql); # excluir pessoa juridica $sql = "DELETE FROM sicae.usuario_pessoa_juridica u WHERE u.sq_usuario_externo = {$sqUsuario}"; $this->_em->getConnection()->exec($sql); # excluir usuario perfil externo $sql = "DELETE FROM sicae.usuario_externo_perfil u WHERE u.sq_usuario_externo = {$sqUsuario}"; $this->_em->getConnection()->exec($sql); # excluir usuario externo $sql = "DELETE FROM sicae.usuario_externo u WHERE u.sq_usuario_externo = {$sqUsuario}"; $this->_em->getConnection()->exec($sql); } }
/** * Acquire a lock on the given entity. * * @param object $entity * @param int $lockMode * @param int $lockVersion */ public function lock($entity, $lockMode, $lockVersion = null) { if ($this->getEntityState($entity) != self::STATE_MANAGED) { throw new InvalidArgumentException("Entity is not MANAGED."); } $entityName = get_class($entity); $class = $this->em->getClassMetadata($entityName); if ($lockMode == \Doctrine\DBAL\LockMode::OPTIMISTIC) { if (!$class->isVersioned) { throw OptimisticLockException::notVersioned($entityName); } if ($lockVersion != null) { $entityVersion = $class->reflFields[$class->versionField]->getValue($entity); if ($entityVersion != $lockVersion) { throw OptimisticLockException::lockFailedVersionMissmatch($entity, $lockVersion, $entityVersion); } } } else { if (in_array($lockMode, array(\Doctrine\DBAL\LockMode::PESSIMISTIC_READ, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE))) { if (!$this->em->getConnection()->isTransactionActive()) { throw TransactionRequiredException::transactionRequired(); } $oid = spl_object_hash($entity); $this->getEntityPersister($class->name)->lock(array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), $lockMode); } } }
/** * Commits the UnitOfWork, executing all operations that have been postponed * up to this point. The state of all managed entities will be synchronized with * the database. * * The operations are executed in the following order: * * 1) All entity insertions * 2) All entity updates * 3) All collection deletions * 4) All collection updates * 5) All entity deletions * */ public function commit() { // Compute changes done since last commit. $this->computeChangeSets(); if (!($this->_entityInsertions || $this->_entityDeletions || $this->_entityUpdates || $this->_collectionUpdates || $this->_collectionDeletions || $this->_orphanRemovals)) { return; // Nothing to do. } if ($this->_orphanRemovals) { foreach ($this->_orphanRemovals as $orphan) { $this->remove($orphan); } } // Raise onFlush if ($this->_evm->hasListeners(Events::onFlush)) { $this->_evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->_em)); } // Now we need a commit order to maintain referential integrity $commitOrder = $this->_getCommitOrder(); $conn = $this->_em->getConnection(); $conn->beginTransaction(); try { if ($this->_entityInsertions) { foreach ($commitOrder as $class) { $this->_executeInserts($class); } } if ($this->_entityUpdates) { foreach ($commitOrder as $class) { $this->_executeUpdates($class); } } // Extra updates that were requested by persisters. if ($this->_extraUpdates) { $this->_executeExtraUpdates(); } // Collection deletions (deletions of complete collections) foreach ($this->_collectionDeletions as $collectionToDelete) { $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); } // Collection updates (deleteRows, updateRows, insertRows) foreach ($this->_collectionUpdates as $collectionToUpdate) { $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); } // Entity deletions come last and need to be in reverse commit order if ($this->_entityDeletions) { for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { $this->_executeDeletions($commitOrder[$i]); } } $conn->commit(); } catch (\Exception $e) { $conn->setRollbackOnly(); $conn->rollback(); $this->_em->close(); throw $e; } // Take new snapshots from visited collections foreach ($this->_visitedCollections as $coll) { $coll->takeSnapshot(); } // Clear up $this->_entityInsertions = $this->_entityUpdates = $this->_entityDeletions = $this->_extraUpdates = $this->_entityChangeSets = $this->_collectionUpdates = $this->_collectionDeletions = $this->_visitedCollections = $this->_scheduledForDirtyCheck = $this->_orphanRemovals = array(); }
/** * @chainable */ public function stopDebug() { $this->em->getConnection()->getConfiguration()->setSQLLogger(NULL); return $this; }
/** * Get the abstract database platform. * * @return Doctrine\DBAL\Platforms\AbstractPlatform */ protected function get_platform() { return $this->em->getConnection()->getDatabasePlatform(); }