diff --git a/src/ObjectMapper.php b/src/ObjectMapper.php index e77cfca..97e0b2d 100644 --- a/src/ObjectMapper.php +++ b/src/ObjectMapper.php @@ -183,6 +183,11 @@ public function findOneBy(string $objectName, array $criteria, ?array $orderBy = return $this->getRepository($objectName)->findOneBy($criteria, $orderBy); } + public function count(string $objectName, array $criteria = []): int + { + return $this->getRepository($objectName)->count($criteria); + } + /** * Load one or more relationship on the provided objects. * diff --git a/src/Repository/ObjectRepository.php b/src/Repository/ObjectRepository.php index 47e32f2..dba6958 100644 --- a/src/Repository/ObjectRepository.php +++ b/src/Repository/ObjectRepository.php @@ -125,6 +125,12 @@ public function findOneBy(array $criteria, array $orderBy = []): ?object return $this->fetchOne($qb); } + public function count(array $criteria = []): int + { + $qb = $this->queryHelper->buildSelectQuery($this->getTableName(), 'main.*', $criteria); + return $this->queryHelper->getCount($qb, $this->objectManager->getIdColumn()); + } + public function getClassName(): string { if ($this->className) { diff --git a/src/Repository/ObjectRepositoryInterface.php b/src/Repository/ObjectRepositoryInterface.php index a3cbda5..b54a81e 100644 --- a/src/Repository/ObjectRepositoryInterface.php +++ b/src/Repository/ObjectRepositoryInterface.php @@ -74,6 +74,13 @@ public function find(string|int $id, bool $useCache = true): ?object; */ public function findByIds(array $ids, bool $useCache = true): array; + /** + * Count the number of rows that meet the provided criteria. + * + * @param array $criteria column => value pairs to be used to build where clause + */ + public function count(array $criteria = []): int; + /** * Return the database table for an object * diff --git a/test/Integration/BasicsTest.php b/test/Integration/BasicsTest.php index 6c6daca..f6260b5 100644 --- a/test/Integration/BasicsTest.php +++ b/test/Integration/BasicsTest.php @@ -43,6 +43,12 @@ public function testFindNull(): void $this->assertNull($fromDb); } + #[Depends('testSave')] + public function testCount() + { + $this->assertGreaterThan(0, $this->repository->count()); + } + /** * @return ExtendedDataObject */ diff --git a/test/Unit/ObjectMapperTest.php b/test/Unit/ObjectMapperTest.php index 49a8622..86a7c0a 100644 --- a/test/Unit/ObjectMapperTest.php +++ b/test/Unit/ObjectMapperTest.php @@ -110,6 +110,18 @@ public function testFindOneBy(): void $this->getCorma($mockRepo)->findOneBy(ExtendedDataObject::class, ['asdf'=>'value'], ['asdf'=>'ASC']); } + public function testCount() + { + $mockRepo = $this->getMockBuilder(ExtendedDataObjectRepository::class) + ->disableOriginalConstructor() + ->getMock(); + + $mockRepo->expects($this->once())->method('count')->with(['asdf'=>'value'])->willReturn(42); + + $count = $this->getCorma($mockRepo)->count(ExtendedDataObject::class, ['asdf'=>'value']); + $this->assertEquals(42, $count); + } + public function testLoadOneToMany(): void { $objects = [];