From 0c2b67bcf93425efc6fecca989fe08796b4ac77f Mon Sep 17 00:00:00 2001 From: Thomas Beaujean Date: Tue, 3 Jun 2025 16:52:24 +0200 Subject: [PATCH] Generate countByXxx methods --- .../Generator/AssociationTemplate.html.twig | 17 +++++ .../views/Generator/ColumnTemplate.html.twig | 17 +++++ tests/Repository/MyClassRepositoryBase.php | 68 +++++++++++++++++++ .../Service/ExpectedMyClassRepositoryBase.txt | 68 +++++++++++++++++++ 4 files changed, 170 insertions(+) diff --git a/src/Resources/views/Generator/AssociationTemplate.html.twig b/src/Resources/views/Generator/AssociationTemplate.html.twig index a25ed82..0e224e9 100644 --- a/src/Resources/views/Generator/AssociationTemplate.html.twig +++ b/src/Resources/views/Generator/AssociationTemplate.html.twig @@ -179,6 +179,23 @@ ); } + public function countBy{{ column }}( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterBy{{ column }}($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function deleteBy{{ column }}( mixed $value, ): void { diff --git a/src/Resources/views/Generator/ColumnTemplate.html.twig b/src/Resources/views/Generator/ColumnTemplate.html.twig index d0664f8..09fddf6 100644 --- a/src/Resources/views/Generator/ColumnTemplate.html.twig +++ b/src/Resources/views/Generator/ColumnTemplate.html.twig @@ -107,6 +107,23 @@ ); } + public function countBy{{ column }}( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterBy{{ column }}($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsBy{{ column }}( mixed $value, bool $useQueryCache = false, diff --git a/tests/Repository/MyClassRepositoryBase.php b/tests/Repository/MyClassRepositoryBase.php index 342f8c7..de7373c 100644 --- a/tests/Repository/MyClassRepositoryBase.php +++ b/tests/Repository/MyClassRepositoryBase.php @@ -458,6 +458,23 @@ public function findOneById( ); } + public function countById( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterById($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsById( mixed $value, bool $useQueryCache = false, @@ -593,6 +610,23 @@ public function findOneByNumber( ); } + public function countByNumber( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByNumber($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsByNumber( mixed $value, bool $useQueryCache = false, @@ -728,6 +762,23 @@ public function findOneByName( ); } + public function countByName( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByName($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsByName( mixed $value, bool $useQueryCache = false, @@ -929,6 +980,23 @@ public function findOneByForeignClasses( ); } + public function countByForeignClasses( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByForeignClasses($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function deleteByForeignClasses( mixed $value, ): void { diff --git a/tests/Service/ExpectedMyClassRepositoryBase.txt b/tests/Service/ExpectedMyClassRepositoryBase.txt index 342f8c7..de7373c 100644 --- a/tests/Service/ExpectedMyClassRepositoryBase.txt +++ b/tests/Service/ExpectedMyClassRepositoryBase.txt @@ -458,6 +458,23 @@ class MyClassRepositoryBase extends \Doctrine\Bundle\DoctrineBundle\Repository\S ); } + public function countById( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterById($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsById( mixed $value, bool $useQueryCache = false, @@ -593,6 +610,23 @@ class MyClassRepositoryBase extends \Doctrine\Bundle\DoctrineBundle\Repository\S ); } + public function countByNumber( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByNumber($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsByNumber( mixed $value, bool $useQueryCache = false, @@ -728,6 +762,23 @@ class MyClassRepositoryBase extends \Doctrine\Bundle\DoctrineBundle\Repository\S ); } + public function countByName( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByName($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function existsByName( mixed $value, bool $useQueryCache = false, @@ -929,6 +980,23 @@ class MyClassRepositoryBase extends \Doctrine\Bundle\DoctrineBundle\Repository\S ); } + public function countByForeignClasses( + mixed $value, + bool $useQueryCache = false, + ?string $cacheId = null, + array $resultCacheTags = [], + ): int { + $qb = $this->getNewQueryBuilder(); + static::filterByForeignClasses($qb, $value); + + return static::getQueryBuilderCount( + qb: $qb, + useQueryCache: $useQueryCache, + cacheId: $cacheId, + resultCacheTags: $resultCacheTags, + ); + } + public function deleteByForeignClasses( mixed $value, ): void {