From 007eb5e2cfd152b5435d2f0e3a5a73c092bc158f Mon Sep 17 00:00:00 2001 From: smarcet Date: Thu, 12 Sep 2019 17:39:23 -0300 Subject: [PATCH] Refactoring on Base ORM classes Change-Id: Iebbb50e150b0610e06df5accfae16389b6750dfe --- app/Repositories/ConfigDoctrineRepository.php | 4 +- app/Repositories/DoctrineRepository.php | 175 +++++++++++++++++- .../Main/DoctrineFolderRepository.php | 12 +- .../SilverStripeDoctrineRepository.php | 7 +- ...ineSpeakerOrganizationalRoleRepository.php | 4 +- .../Summit/DoctrineSpeakerRepository.php | 12 +- ...DoctrineSummitAttendeeTicketRepository.php | 2 +- .../DoctrineSummitEntityEventRepository.php | 8 +- .../Summit/DoctrineSummitEventRepository.php | 4 +- .../Utils/DoctrineTransactionService.php | 1 - 10 files changed, 193 insertions(+), 36 deletions(-) diff --git a/app/Repositories/ConfigDoctrineRepository.php b/app/Repositories/ConfigDoctrineRepository.php index 1f892e2c..0a79470a 100644 --- a/app/Repositories/ConfigDoctrineRepository.php +++ b/app/Repositories/ConfigDoctrineRepository.php @@ -29,7 +29,7 @@ abstract class ConfigDoctrineRepository extends DoctrineRepository */ public function __construct($em, ClassMetadata $class) { - $em = Registry::getManager(ResourceServerEntity::EntityManager); - parent::__construct($em, $class); + $this->manager_name = ResourceServerEntity::EntityManager; + parent::__construct(Registry::getManager($this->manager_name), $class); } } \ No newline at end of file diff --git a/app/Repositories/DoctrineRepository.php b/app/Repositories/DoctrineRepository.php index 9bea3384..e9a16fdf 100644 --- a/app/Repositories/DoctrineRepository.php +++ b/app/Repositories/DoctrineRepository.php @@ -11,19 +11,22 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\ORM\NativeQuery; +use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use LaravelDoctrine\ORM\Facades\Registry; use models\utils\IBaseRepository; use models\utils\IEntity; -use models\utils\SilverstripeBaseModel; +use Doctrine\ORM\Query\ResultSetMappingBuilder; use utils\Filter; use utils\Order; use utils\PagingInfo; use utils\PagingResponse; use Doctrine\ORM\Tools\Pagination\Paginator; +use Doctrine\ORM\LazyCriteriaCollection; /** * Class DoctrineRepository * @package App\Repositories @@ -32,9 +35,17 @@ abstract class DoctrineRepository extends EntityRepository implements IBaseRepos { /** - * @param int $id - * @return IEntity|null|object + * @var string */ + protected $manager_name; + /** + * @return EntityManager + */ + protected function getEntityManager() + { + return Registry::getManager($this->manager_name); + } + public function getById($id) { return $this->find($id); @@ -57,9 +68,9 @@ abstract class DoctrineRepository extends EntityRepository implements IBaseRepos */ public function add($entity, $sync = false) { - $this->_em->persist($entity); + $this->getEntityManager()->persist($entity); if($sync) - $this->_em->flush($entity); + $this->getEntityManager()->flush($entity); } /** @@ -68,7 +79,7 @@ abstract class DoctrineRepository extends EntityRepository implements IBaseRepos */ public function delete($entity) { - $this->_em->remove($entity); + $this->getEntityManager()->remove($entity); } /** @@ -144,4 +155,152 @@ abstract class DoctrineRepository extends EntityRepository implements IBaseRepos ); } -} \ No newline at end of file + /** + * Creates a new QueryBuilder instance that is prepopulated for this entity name. + * + * @param string $alias + * @param string $indexBy The index for the from. + * + * @return QueryBuilder + */ + public function createQueryBuilder($alias, $indexBy = null) + { + return $this->getEntityManager()->createQueryBuilder() + ->select($alias) + ->from($this->_entityName, $alias, $indexBy); + } + + /** + * Creates a new result set mapping builder for this entity. + * + * The column naming strategy is "INCREMENT". + * + * @param string $alias + * + * @return ResultSetMappingBuilder + */ + public function createResultSetMappingBuilder($alias) + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager(), ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); + $rsm->addRootEntityFromClassMetadata($this->_entityName, $alias); + + return $rsm; + } + + /** + * Creates a new Query instance based on a predefined metadata named query. + * + * @param string $queryName + * + * @return Query + */ + public function createNamedQuery($queryName) + { + return $this->getEntityManager()->createQuery($this->_class->getNamedQuery($queryName)); + } + + /** + * Creates a native SQL query. + * + * @param string $queryName + * + * @return NativeQuery + */ + public function createNativeNamedQuery($queryName) + { + $queryMapping = $this->_class->getNamedNativeQuery($queryName); + $rsm = new Query\ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addNamedNativeQueryMapping($this->_class, $queryMapping); + + return $this->getEntityManager()->createNativeQuery($queryMapping['query'], $rsm); + } + + /** + * Clears the repository, causing all managed entities to become detached. + * + * @return void + */ + public function clear() + { + $this->getEntityManager()->clear($this->_class->rootEntityName); + } + + /** + * Finds an entity by its primary key / identifier. + * + * @param mixed $id The identifier. + * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants + * or NULL if no specific lock mode should be used + * during the search. + * @param int|null $lockVersion The lock version. + * + * @return object|null The entity instance or NULL if the entity can not be found. + */ + public function find($id, $lockMode = null, $lockVersion = null) + { + return $this->getEntityManager()->find($this->_entityName, $id, $lockMode, $lockVersion); + } + + /** + * Finds entities by a set of criteria. + * + * @param array $criteria + * @param array|null $orderBy + * @param int|null $limit + * @param int|null $offset + * + * @return array The objects. + */ + public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + { + $persister = $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName); + + return $persister->loadAll($criteria, $orderBy, $limit, $offset); + } + + /** + * Finds a single entity by a set of criteria. + * + * @param array $criteria + * @param array|null $orderBy + * + * @return object|null The entity instance or NULL if the entity can not be found. + */ + public function findOneBy(array $criteria, array $orderBy = null) + { + $persister = $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName); + + return $persister->load($criteria, null, null, [], null, 1, $orderBy); + } + + /** + * Counts entities by a set of criteria. + * + * @todo Add this method to `ObjectRepository` interface in the next major release + * + * @param array $criteria + * + * @return int The cardinality of the objects that match the given criteria. + */ + public function count(array $criteria) + { + return $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName)->count($criteria); + } + + /** + * Select all elements from a selectable that match the expression and + * return a new collection containing these elements. + * + * @param \Doctrine\Common\Collections\Criteria $criteria + * + * @return \Doctrine\Common\Collections\Collection + */ + public function matching(Criteria $criteria) + { + $persister = $this->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_entityName); + + return new LazyCriteriaCollection($persister, $criteria); + } + + +} diff --git a/app/Repositories/Main/DoctrineFolderRepository.php b/app/Repositories/Main/DoctrineFolderRepository.php index f802202b..b77a17ca 100644 --- a/app/Repositories/Main/DoctrineFolderRepository.php +++ b/app/Repositories/Main/DoctrineFolderRepository.php @@ -36,9 +36,9 @@ final class DoctrineFolderRepository Name = :folder_name LIMIT 0,1; SQL; // build rsm here - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\main\File::class, 'f'); - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); $native_query->setParameter("folder_name", $folder_name); @@ -57,9 +57,9 @@ SQL; FileName = :file_name LIMIT 0,1; SQL; // build rsm here - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\main\File::class, 'f'); - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); $native_query->setParameter("file_name", $file_name); @@ -86,9 +86,9 @@ SQL; Name = :folder_name and ParentID = :parent_id LIMIT 0,1; SQL; // build rsm here - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\main\File::class, 'f'); - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); $native_query->setParameter("folder_name", $folder_name); $native_query->setParameter("parent_id", $parent->getId()); diff --git a/app/Repositories/SilverStripeDoctrineRepository.php b/app/Repositories/SilverStripeDoctrineRepository.php index 1b256e1b..b21eab6b 100644 --- a/app/Repositories/SilverStripeDoctrineRepository.php +++ b/app/Repositories/SilverStripeDoctrineRepository.php @@ -32,8 +32,8 @@ abstract class SilverStripeDoctrineRepository extends DoctrineRepository */ public function __construct($em, ClassMetadata $class) { - $em = Registry::getManager(SilverstripeBaseModel::EntityManager); - parent::__construct($em, $class); + $this->manager_name = SilverstripeBaseModel::EntityManager; + parent::__construct(Registry::getManager(SilverstripeBaseModel::EntityManager), $class); } /** @@ -68,7 +68,6 @@ abstract class SilverStripeDoctrineRepository extends DoctrineRepository $resource_server_ctx = App::make(\models\oauth2\IResourceServerContext::class); $member = $resource_server_ctx->getCurrentUser(); if(is_null($member)) return false; - return $member->isOnGroup($group_code); - return false; + return $member->isOnGroup($group_code); } } \ No newline at end of file diff --git a/app/Repositories/Summit/DoctrineSpeakerOrganizationalRoleRepository.php b/app/Repositories/Summit/DoctrineSpeakerOrganizationalRoleRepository.php index b317e789..2c699133 100644 --- a/app/Repositories/Summit/DoctrineSpeakerOrganizationalRoleRepository.php +++ b/app/Repositories/Summit/DoctrineSpeakerOrganizationalRoleRepository.php @@ -51,9 +51,9 @@ final class DoctrineSpeakerOrganizationalRoleRepository Role = :role SQL; // build rsm here - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(SpeakerOrganizationalRole::class, 'r'); - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); $native_query->setParameter("role", $role); diff --git a/app/Repositories/Summit/DoctrineSpeakerRepository.php b/app/Repositories/Summit/DoctrineSpeakerRepository.php index 2bf9a75a..82468336 100644 --- a/app/Repositories/Summit/DoctrineSpeakerRepository.php +++ b/app/Repositories/Summit/DoctrineSpeakerRepository.php @@ -145,7 +145,7 @@ SUMMIT_SPEAKERS SQL; - $stm = $this->_em->getConnection()->executeQuery($query_count, $bindings); + $stm = $this->getEntityManager()->getConnection()->executeQuery($query_count, $bindings); $total = intval($stm->fetchColumn(0)); @@ -309,11 +309,11 @@ SQL; $rsm->addFieldResult('p', 'PhotoName', 'name'); $rsm->addFieldResult('m', 'MemberID', 'id');*/ - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\summit\PresentationSpeaker::class, 's', ['Title' => 'SpeakerTitle']); // build rsm here - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); foreach($bindings as $k => $v) $native_query->setParameter($k, $v); @@ -382,7 +382,7 @@ SUMMIT_SPEAKERS SQL; - $stm = $this->_em->getConnection()->executeQuery($query_count, $bindings); + $stm = $this->getEntityManager()->getConnection()->executeQuery($query_count, $bindings); $total = intval($stm->fetchColumn(0)); @@ -441,11 +441,11 @@ SQL; $rsm->addFieldResult('p', 'PhotoName', 'name'); $rsm->addFieldResult('m', 'MemberID', 'id');*/ - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\summit\PresentationSpeaker::class, 's', ['Title' => 'SpeakerTitle']); // build rsm here - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); foreach($bindings as $k => $v) $native_query->setParameter($k, $v); diff --git a/app/Repositories/Summit/DoctrineSummitAttendeeTicketRepository.php b/app/Repositories/Summit/DoctrineSummitAttendeeTicketRepository.php index a9277a90..f8f417b9 100644 --- a/app/Repositories/Summit/DoctrineSummitAttendeeTicketRepository.php +++ b/app/Repositories/Summit/DoctrineSummitAttendeeTicketRepository.php @@ -59,7 +59,7 @@ final class DoctrineSummitAttendeeTicketRepository */ public function delete($entity) { - $this->_em->getConnection()->delete(" + $this->getEntityManager()->getConnection()->delete(" SummitAttendeeTicket ", ["ID" => $entity->getIdentifier()]); } diff --git a/app/Repositories/Summit/DoctrineSummitEntityEventRepository.php b/app/Repositories/Summit/DoctrineSummitEntityEventRepository.php index 4b212a10..8a6bdaac 100644 --- a/app/Repositories/Summit/DoctrineSummitEntityEventRepository.php +++ b/app/Repositories/Summit/DoctrineSummitEntityEventRepository.php @@ -111,14 +111,14 @@ SQL; ORDER BY Created ASC LIMIT {$limit}; SQL; - $rsm = new ResultSetMappingBuilder($this->_em); + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); $rsm->addRootEntityFromClassMetadata(\models\summit\SummitEntityEvent::class, 'e'); // build rsm here - $native_query = $this->_em->createNativeQuery($query, $rsm); + $native_query = $this->getEntityManager()->createNativeQuery($query, $rsm); $entity_events = $native_query->getResult(); - if($detach) $this->_em ->clear(\models\summit\SummitEntityEvent::class); + if($detach) $this->getEntityManager()->clear(\models\summit\SummitEntityEvent::class); return $entity_events; } @@ -133,7 +133,7 @@ SQL; SELECT ID FROM SummitEntityEvent WHERE SummitID = {$summit->getId()} ORDER BY ID DESC LIMIT 1; SQL; - return intval($this->_em->getConnection()->executeQuery($query)->fetchColumn(0)); + return intval($this->getEntityManager()->getConnection()->executeQuery($query)->fetchColumn(0)); } /** diff --git a/app/Repositories/Summit/DoctrineSummitEventRepository.php b/app/Repositories/Summit/DoctrineSummitEventRepository.php index 98efce88..73dcc5a9 100644 --- a/app/Repositories/Summit/DoctrineSummitEventRepository.php +++ b/app/Repositories/Summit/DoctrineSummitEventRepository.php @@ -264,10 +264,10 @@ final class DoctrineSummitEventRepository public function cleanupScheduleAndFavoritesForEvent($event_id){ $query = "DELETE Member_Schedule FROM Member_Schedule WHERE SummitEventID = {$event_id};"; - $this->_em->getConnection()->executeUpdate($query); + $this->getEntityManager()->getConnection()->executeUpdate($query); $query = "DELETE `Member_FavoriteSummitEvents` FROM `Member_FavoriteSummitEvents` WHERE SummitEventID = {$event_id};"; - $this->_em->getConnection()->executeUpdate($query); + $this->getEntityManager()->getConnection()->executeUpdate($query); } /** diff --git a/app/Services/Utils/DoctrineTransactionService.php b/app/Services/Utils/DoctrineTransactionService.php index c895b64f..39c09028 100644 --- a/app/Services/Utils/DoctrineTransactionService.php +++ b/app/Services/Utils/DoctrineTransactionService.php @@ -65,7 +65,6 @@ final class DoctrineTransactionService implements ITransactionService */ if ($con->ping() === false) { - Log::warning("DoctrineTransactionService::transaction: conn is closed... reconecting"); $con->close(); $con->connect(); }