Skip to content

Commit

Permalink
Add count method to repositories and a pass-through on ObjectMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
thewunder committed Nov 15, 2024
1 parent b071c70 commit 6f9bfee
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Repository/ObjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/Repository/ObjectRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
6 changes: 6 additions & 0 deletions test/Integration/BasicsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public function testFindNull(): void
$this->assertNull($fromDb);
}

#[Depends('testSave')]
public function testCount()
{
$this->assertGreaterThan(0, $this->repository->count());
}

/**
* @return ExtendedDataObject
*/
Expand Down
12 changes: 12 additions & 0 deletions test/Unit/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 6f9bfee

Please sign in to comment.