Skip to content

Commit ef145f3

Browse files
committed
Added grabNumRecords function
1 parent 628ec7a commit ef145f3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Codeception/Module/Symfony.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,33 @@ public function seeNumRecords(int $expectedNum, string $className, array $criter
840840
);
841841
}
842842

843+
/**
844+
* Retrieves number of records from database
845+
* 'id' is the default search parameter.
846+
*
847+
* ```php
848+
* <?php
849+
* $I->grabNumRecords('User::class', ['name' => 'davert']);
850+
* ```
851+
*
852+
* @param string $entityClass The entity class
853+
* @param array $criteria Optional query criteria
854+
* @return integer
855+
*/
856+
public function grabNumRecords(string $entityClass, array $criteria = [])
857+
{
858+
$em = $this->_getEntityManager();
859+
$repository = $em->getRepository($entityClass);
860+
861+
if (empty($criteria)) {
862+
return (int)$repository->createQueryBuilder('a')
863+
->select('count(a.id)')
864+
->getQuery()
865+
->getSingleScalarResult();
866+
}
867+
return $repository->count($criteria);
868+
}
869+
843870
/**
844871
* Invalidate the current session.
845872
* ```php

0 commit comments

Comments
 (0)