Instantly share code, notes, and snippets.
Last active
March 16, 2017 10:19
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Charlie-Lucas/8739ff36d4e5d4a644e1e5a8d219d930 to your computer and use it in GitHub Desktop.
First one is an exemple to default find record method, seccond is an exemple for custom find record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function getWidgetBusinessEntityContent(Widget $widget) | |
{ | |
$parameters = parent::getWidgetBusinessEntityContent($widget); | |
/** @var WidgetSliderNav $widget */ | |
//get previous and next record if entity is set | |
if ($entity = $widget->getEntity()) { | |
$template = $widget->getTargetPattern(); | |
$repository = $this->entityManager->getRepository(get_class($entity)); | |
$entitiesAllowed = []; | |
if(!$template) | |
{ | |
$template = $this->businessPageHelper->guessBestPatternIdForEntity(new \ReflectionClass($entity), $entity->getId(), $this->entityManager); | |
} | |
if($template) | |
{ | |
$entitiesAllowed = $this->businessPageHelper->getEntitiesAllowed($template, $this->entityManager); | |
} | |
try { | |
$previousRecord = self::getPreviousRecord($repository, $entity, $entitiesAllowed); | |
$parameters['previousRecord'] = $previousRecord; | |
} catch (\Exception $e) { | |
$parameters['previousRecord'] = null; | |
} | |
try { | |
$nextRecord = self::getNextRecord($repository, $entity, $entitiesAllowed); | |
$parameters['nextRecord'] = $nextRecord; | |
} catch (\Exception $e) { | |
$parameters['nextRecord'] = null; | |
} | |
} | |
return $parameters; | |
} | |
protected static function getPreviousRecord($repository, $entity, $entitiesAllowed = []) | |
{ | |
//run default method to get previous record | |
if(!count($entitiesAllowed)) | |
{ | |
return null; | |
} | |
$queryBuilder = $repository->createQueryBuilder('a'); | |
$previousRecord = $queryBuilder | |
->where($queryBuilder->expr()->lt('a.id', ':id')) | |
->andWhere($queryBuilder->expr()->in('a', ':entitiesAllowed')) | |
->setParameters([ | |
'id'=> $entity->getId(), | |
'entitiesAllowed' =>$entitiesAllowed | |
]) | |
->orderBy('a.id', 'DESC') | |
->setMaxResults(1) | |
->getQuery() | |
->getOneOrNullResult(); | |
//if there isn't any previous record, we try to get the very last result | |
if (!$previousRecord) { | |
//run default method to get the very last record | |
$queryBuilder = $repository->createQueryBuilder('a'); | |
$previousRecord = $queryBuilder | |
->where($queryBuilder->expr()->in('a', ':entitiesAllowed')) | |
->orderBy('a.id', 'DESC') | |
->setParameter('entitiesAllowed', $entitiesAllowed) | |
->setMaxResults(1) | |
->getQuery() | |
->getOneOrNullResult(); | |
//if the last record is the same as the entity, we set null | |
if ($previousRecord === $entity) { | |
$previousRecord = null; | |
} | |
} | |
return $previousRecord; | |
} | |
/** | |
* @param \Doctrine\ORM\EntityRepository $repository | |
* @param mixed $entity | |
* @param array $entitiesAllowed | |
* @return mixed|null | |
* @throws \Doctrine\ORM\NonUniqueResultException | |
*/ | |
protected static function getNextRecord($repository, $entity, $entitiesAllowed = []) | |
{ | |
//check if an overriden method exists | |
//run default method to get next record | |
if(!count($entitiesAllowed)) | |
{ | |
return null; | |
} | |
$queryBuilder = $repository->createQueryBuilder('a'); | |
$nextRecord = $queryBuilder | |
->where($queryBuilder->expr()->gt('a.id', ':id')) | |
->andWhere($queryBuilder->expr()->in('a', ':entitiesAllowed')) | |
->setParameters([ | |
'id'=> $entity->getId(), | |
'entitiesAllowed' =>$entitiesAllowed | |
]) | |
->orderBy('a.id', 'ASC') | |
->setMaxResults(1) | |
->getQuery() | |
->getOneOrNullResult(); | |
//if there isn't any next record, we try to get the very first result | |
if (!$nextRecord) { | |
//run default method to get the very first record | |
$queryBuilder = $repository->createQueryBuilder('a'); | |
$nextRecord = $queryBuilder | |
->where($queryBuilder->expr()->in('a', ':entitiesAllowed')) | |
->orderBy('a.id', 'ASC') | |
->setParameter('entitiesAllowed', $entitiesAllowed) | |
->setMaxResults(1) | |
->getQuery() | |
->getOneOrNullResult(); | |
//if the first record is the same as the entity, we set null | |
if ($nextRecord === $entity) { | |
$nextRecord = null; | |
} | |
} | |
return $nextRecord; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected static function getPreviousRecord($repository, $entity, $entitiesAllowed = []) | |
{ | |
if (method_exists($repository, 'getPreviousRecord')) { | |
$previousRecord = null; | |
$current = $entity; | |
while($previousRecord = null && $current) | |
{ | |
$previous = $repository->getPreviousRecord($current->getId()); | |
if(in_array($previous, $entitiesAllowed)) | |
{ | |
$previousrecord = $previous; | |
} else { | |
$current = $previous; | |
} | |
} | |
} | |
return $previousRecord; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment