Refactoring on Base ORM classes
Change-Id: Iebbb50e150b0610e06df5accfae16389b6750dfe
This commit is contained in:
parent
66af7dc25c
commit
007eb5e2cf
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -59,7 +59,7 @@ final class DoctrineSummitAttendeeTicketRepository
|
||||
*/
|
||||
public function delete($entity)
|
||||
{
|
||||
$this->_em->getConnection()->delete("
|
||||
$this->getEntityManager()->getConnection()->delete("
|
||||
SummitAttendeeTicket
|
||||
", ["ID" => $entity->getIdentifier()]);
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user