From 6e224c63effc5cedd9940dd8f413706125d0cd6e Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Sun, 5 May 2019 03:09:02 +0200 Subject: [PATCH] improve cross database engine compatibility (#756) --- src/Entity/Timesheet.php | 2 +- src/Repository/ActivityRepository.php | 18 ++++--- src/Repository/CustomerRepository.php | 74 +++++++++++++++----------- src/Repository/ProjectRepository.php | 52 +++++++++--------- src/Repository/TimesheetRepository.php | 6 +-- src/Repository/UserRepository.php | 16 +++--- 6 files changed, 97 insertions(+), 71 deletions(-) diff --git a/src/Entity/Timesheet.php b/src/Entity/Timesheet.php index c3a02282..3f3f3200 100644 --- a/src/Entity/Timesheet.php +++ b/src/Entity/Timesheet.php @@ -73,7 +73,7 @@ class Timesheet * @var User * * @ORM\ManyToOne(targetEntity="App\Entity\User") - * @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE", nullable=false) + * @ORM\JoinColumn(name="`user`", referencedColumnName="id", onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ private $user; diff --git a/src/Repository/ActivityRepository.php b/src/Repository/ActivityRepository.php index 643bc05f..79a10101 100644 --- a/src/Repository/ActivityRepository.php +++ b/src/Repository/ActivityRepository.php @@ -48,12 +48,13 @@ class ActivityRepository extends AbstractRepository ->join('t.project', 'p') ->join('p.customer', 'c') ->andWhere($qb->expr()->isNotNull('t.end')) - ->andWhere('a.visible = 1') - ->andWhere('p.visible = 1') - ->andWhere('c.visible = 1') + ->andWhere($qb->expr()->eq('a.visible', ':visible')) + ->andWhere($qb->expr()->eq('p.visible', ':visible')) + ->andWhere($qb->expr()->eq('c.visible', ':visible')) ->groupBy('a.id', 'p.id') ->orderBy('maxid', 'DESC') ->setMaxResults(10) + ->setParameter('visible', true, \PDO::PARAM_BOOL) ; if (null !== $user) { @@ -88,10 +89,15 @@ class ActivityRepository extends AbstractRepository } /** + * @param null|bool $visible * @return int */ - public function countActivity() + public function countActivity($visible = null) { + if (null !== $visible) { + return $this->count(['visible' => (bool) $visible]); + } + return $this->count([]); } @@ -185,10 +191,10 @@ class ActivityRepository extends AbstractRepository ) ); } - $qb->setParameter('visible', 1); + $qb->setParameter('visible', true, \PDO::PARAM_BOOL); } elseif (ActivityQuery::SHOW_HIDDEN == $query->getVisibility()) { $where->add('a.visible = :visible'); - $qb->setParameter('visible', 0); + $qb->setParameter('visible', false, \PDO::PARAM_BOOL); } if ($query->isGlobalsOnly()) { diff --git a/src/Repository/CustomerRepository.php b/src/Repository/CustomerRepository.php index 5cf969a2..5ee6e960 100644 --- a/src/Repository/CustomerRepository.php +++ b/src/Repository/CustomerRepository.php @@ -20,9 +20,6 @@ use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Pagerfanta\Pagerfanta; -/** - * Class CustomerRepository - */ class CustomerRepository extends AbstractRepository { /** @@ -41,7 +38,7 @@ class CustomerRepository extends AbstractRepository public function countCustomer($visible = null) { if (null !== $visible) { - return $this->count(['visible' => (int) $visible]); + return $this->count(['visible' => (bool) $visible]); } return $this->count([]); @@ -55,34 +52,47 @@ class CustomerRepository extends AbstractRepository */ public function getCustomerStatistics(Customer $customer) { - $qb = $this->getEntityManager()->createQueryBuilder(); - - $qb->select('COUNT(t.id) as recordAmount') - ->addSelect('SUM(t.duration) as recordDuration') - ->addSelect('COUNT(DISTINCT(a.id)) as activityAmount') - ->addSelect('COUNT(DISTINCT(p.id)) as projectAmount') - ->from(Timesheet::class, 't') - ->join(Activity::class, 'a') - ->join(Project::class, 'p') - ->join(Customer::class, 'c') - ->andWhere('t.activity = a.id') - ->andWhere('t.project = p.id') - ->andWhere('p.customer = c.id') - ->andWhere('c.id = :customer') - ; - - $result = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY); - $stats = new CustomerStatistic(); + $stats->setCount(1); - if (isset($result[0])) { - $dbStats = $result[0]; + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb + ->addSelect('COUNT(t.id) as recordAmount') + ->addSelect('SUM(t.duration) as recordDuration') + ->from(Timesheet::class, 't') + ->join(Project::class, 'p', Query\Expr\Join::WITH, 't.project = p.id') + ->andWhere('p.customer = :customer') + ; + $timesheetResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY); - $stats->setCount(1); - $stats->setRecordAmount($dbStats['recordAmount']); - $stats->setRecordDuration($dbStats['recordDuration']); - $stats->setActivityAmount($dbStats['activityAmount']); - $stats->setProjectAmount($dbStats['projectAmount']); + if (isset($timesheetResult[0])) { + $stats->setRecordAmount($timesheetResult[0]['recordAmount']); + $stats->setRecordDuration($timesheetResult[0]['recordDuration']); + } + + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb + ->addSelect('COUNT(a.id) as activityAmount') + ->from(Activity::class, 'a') + ->join(Project::class, 'p', Query\Expr\Join::WITH, 'a.project = p.id') + ->andWhere('a.project = p.id') + ->andWhere('p.customer = :customer') + ; + $activityResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY); + + if (isset($activityResult[0])) { + $stats->setActivityAmount($activityResult[0]['activityAmount']); + } + + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb->addSelect('COUNT(p.id) as projectAmount') + ->from(Project::class, 'p') + ->andWhere('p.customer = :customer') + ; + $projectResult = $qb->getQuery()->execute(['customer' => $customer], Query::HYDRATE_ARRAY); + + if (isset($projectResult[0])) { + $stats->setProjectAmount($projectResult[0]['projectAmount']); } return $stats; @@ -117,7 +127,8 @@ class CustomerRepository extends AbstractRepository ->orderBy('c.' . $query->getOrderBy(), $query->getOrder()); if (CustomerQuery::SHOW_VISIBLE == $query->getVisibility()) { - $qb->andWhere('c.visible = 1'); + $qb->andWhere($qb->expr()->eq('c.visible', ':visible')); + $qb->setParameter('visible', true, \PDO::PARAM_BOOL); /** @var Customer $entity */ $entity = $query->getHiddenEntity(); @@ -125,7 +136,8 @@ class CustomerRepository extends AbstractRepository $qb->orWhere('c.id = :customer')->setParameter('customer', $entity); } } elseif (CustomerQuery::SHOW_HIDDEN == $query->getVisibility()) { - $qb->andWhere('c.visible = 0'); + $qb->andWhere($qb->expr()->eq('c.visible', ':visible')); + $qb->setParameter('visible', false, \PDO::PARAM_BOOL); } if (!empty($query->getIgnoredEntities())) { diff --git a/src/Repository/ProjectRepository.php b/src/Repository/ProjectRepository.php index de2698aa..58669a5c 100644 --- a/src/Repository/ProjectRepository.php +++ b/src/Repository/ProjectRepository.php @@ -41,42 +41,44 @@ class ProjectRepository extends AbstractRepository public function countProject($visible = null) { if (null !== $visible) { - return $this->count(['visible' => (int) $visible]); + return $this->count(['visible' => (bool) $visible]); } return $this->count([]); } - /** - * Retrieves statistics for one project. - * - * @param Project $project - * @return ProjectStatistic - */ - public function getProjectStatistics(Project $project) + public function getProjectStatistics(Project $project): ProjectStatistic { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('COUNT(t.id) as recordAmount') ->addSelect('SUM(t.duration) as recordDuration') - ->addSelect('COUNT(DISTINCT(a.id)) as activityAmount') - ->from(Activity::class, 'a') - ->join(Timesheet::class, 't') - ->where('t.project = :project') - ->andWhere('t.activity = a.id') + ->from(Timesheet::class, 't') + ->andWhere('t.project = :project') ; + $resultTimesheets = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY); - $result = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY); + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb->select('COUNT(a.id) as activityAmount') + ->from(Activity::class, 'a') + ->andWhere('a.project = :project') + ; + $resultActivities = $qb->getQuery()->execute(['project' => $project], Query::HYDRATE_ARRAY); $stats = new ProjectStatistic(); + $stats->setCount(1); - if (isset($result[0])) { - $dbStats = $result[0]; + if (isset($resultTimesheets[0])) { + $resultTimesheets = $resultTimesheets[0]; - $stats->setCount(1); - $stats->setRecordAmount($dbStats['recordAmount']); - $stats->setRecordDuration($dbStats['recordDuration']); - $stats->setActivityAmount($dbStats['activityAmount']); + $stats->setRecordAmount($resultTimesheets['recordAmount']); + $stats->setRecordDuration($resultTimesheets['recordDuration']); + } + + if (isset($resultActivities[0])) { + $resultActivities = $resultActivities[0]; + + $stats->setActivityAmount($resultActivities['activityAmount']); } return $stats; @@ -108,7 +110,7 @@ class ProjectRepository extends AbstractRepository { $qb = $this->getEntityManager()->createQueryBuilder(); - // if we join activities, the maxperpage limit will limit the list + // if we join activities, the max-per-page limit will limit the list // due to the raised amount of rows by projects * activities $qb->select('p', 'c') ->from(Project::class, 'p') @@ -117,9 +119,10 @@ class ProjectRepository extends AbstractRepository if (ProjectQuery::SHOW_VISIBLE == $query->getVisibility()) { if (!$query->isExclusiveVisibility()) { - $qb->andWhere('c.visible = 1'); + $qb->andWhere($qb->expr()->eq('c.visible', ':visible')); } - $qb->andWhere('p.visible = 1'); + $qb->andWhere($qb->expr()->eq('p.visible', ':visible')); + $qb->setParameter('visible', true, \PDO::PARAM_BOOL); $entity = $query->getHiddenEntity(); if (null !== $entity) { @@ -128,7 +131,8 @@ class ProjectRepository extends AbstractRepository // TODO check for visibility of customer } elseif (ProjectQuery::SHOW_HIDDEN == $query->getVisibility()) { - $qb->andWhere('p.visible = 0'); + $qb->andWhere($qb->expr()->eq('p.visible', ':visible')); + $qb->setParameter('visible', false, \PDO::PARAM_BOOL); // TODO check for visibility of customer } diff --git a/src/Repository/TimesheetRepository.php b/src/Repository/TimesheetRepository.php index b69f4f4d..2d678c64 100644 --- a/src/Repository/TimesheetRepository.php +++ b/src/Repository/TimesheetRepository.php @@ -212,13 +212,13 @@ class TimesheetRepository extends AbstractRepository $qb->select('SUM(t.rate) as rate, SUM(t.duration) as duration, MONTH(t.begin) as month, YEAR(t.begin) as year') ->from(Timesheet::class, 't') - ->where($qb->expr()->gt('t.begin', ':from')) ; if (!empty($begin)) { + $qb->where($qb->expr()->gt('t.begin', ':from')); $qb->setParameter('from', $begin, Type::DATETIME); } else { - $qb->setParameter('from', 0); + $qb->where($qb->expr()->isNotNull('t.begin')); } if (!empty($end)) { @@ -274,7 +274,7 @@ class TimesheetRepository extends AbstractRepository ->join('t.activity', 'a') ->join('t.project', 'p') ->join('p.customer', 'c') - ->where($qb->expr()->gt('t.begin', '0')) + ->where($qb->expr()->isNotNull('t.begin')) ->andWhere($qb->expr()->isNull('t.end')) ->orderBy('t.begin', 'DESC'); diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 87de80af..ed3b8a15 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -13,9 +13,6 @@ use App\Entity\User; use App\Repository\Query\UserQuery; use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface; -/** - * Class UserRepository - */ class UserRepository extends AbstractRepository implements UserLoaderInterface { /** @@ -41,10 +38,15 @@ class UserRepository extends AbstractRepository implements UserLoaderInterface } /** + * @param null|bool $enabled * @return int */ - public function countUser() + public function countUser($enabled = null) { + if (null !== $enabled) { + return $this->count(['enabled' => (bool) $enabled]); + } + return $this->count([]); } @@ -61,9 +63,11 @@ class UserRepository extends AbstractRepository implements UserLoaderInterface ->orderBy('u.' . $query->getOrderBy(), $query->getOrder()); if (UserQuery::SHOW_VISIBLE == $query->getVisibility()) { - $qb->andWhere('u.enabled = 1'); + $qb->andWhere($qb->expr()->eq('u.enabled', ':enabled')); + $qb->setParameter('enabled', true, \PDO::PARAM_BOOL); } elseif (UserQuery::SHOW_HIDDEN == $query->getVisibility()) { - $qb->andWhere('u.enabled = 0'); + $qb->andWhere($qb->expr()->eq('u.enabled', ':enabled')); + $qb->setParameter('enabled', false, \PDO::PARAM_BOOL); } if ($query->getRole() !== null) {