Skip to content

Commit

Permalink
Add cacheResult to repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasbeaujean committed Sep 23, 2024
1 parent 170c9b0 commit f69d784
Show file tree
Hide file tree
Showing 5 changed files with 1,114 additions and 284 deletions.
83 changes: 67 additions & 16 deletions src/Resources/views/Generator/AssociationTemplate.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

public static function filterBy{{ column }}(QueryBuilder $qb, $value, $operator = Comparison::EQ, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterBy{{ column }}(
QueryBuilder $qb,
$value,
$operator = Comparison::EQ,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
//get a uniq index
$index = static::getParameterIndex();
$parameterName = $columnName.$index;
Expand Down Expand Up @@ -41,8 +46,12 @@
return $qb;
}

public static function filterIn{{ column }}(QueryBuilder $qb, $values, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterIn{{ column }}(
QueryBuilder $qb,
$values,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
//get a uniq index
$index = static::getParameterIndex();
$parameterName = $columnName.$index;
Expand Down Expand Up @@ -77,8 +86,12 @@
return $qb;
}

public static function filterNotIn{{ column }}(QueryBuilder $qb, $values, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterNotIn{{ column }}(
QueryBuilder $qb,
$values,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
//get a uniq index
$index = static::getParameterIndex();
$parameterName = $columnName.$index;
Expand All @@ -100,40 +113,70 @@
return $qb;
}

public static function join{{ column }}(QueryBuilder $qb, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}', $entityDqlTargeted = '{{ entityDqlTargeted }}'): QueryBuilder
{
public static function join{{ column }}(
QueryBuilder $qb,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
$entityDqlTargeted = '{{ entityDqlTargeted }}',
): QueryBuilder {
$qb->join($entityName.'.'.$columnName, $entityDqlTargeted);

return $qb;
}

public static function leftJoin{{ column }}(QueryBuilder $qb, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}', $entityDqlTargeted = '{{ entityDqlTargeted }}'): QueryBuilder
{
public static function leftJoin{{ column }}(
QueryBuilder $qb,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
$entityDqlTargeted = '{{ entityDqlTargeted }}',
): QueryBuilder {
$qb->leftJoin($entityName.'.'.$columnName, $entityDqlTargeted);

return $qb;
}

public function findBy{{ column }}(mixed $value): array
{
public function findBy{{ column }}(
mixed $value,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): array {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

return static::getQueryBuilderResult($qb);
return $this->getQueryBuilderResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

public function findOneBy{{ column }}(
mixed $value,
bool $allowNull = false,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): ?\{{ entityClasspath }} {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

if ($allowNull) {
return static::getQueryBuilderOneOrNullResult($qb);
return $this->getQueryBuilderOneOrNullResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

return static::getQueryBuilderSingleResult($qb);
return $this->getQueryBuilderSingleResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

public function deleteBy{{ column }}(
Expand All @@ -147,9 +190,17 @@

public function existsBy{{ column }}(
mixed $value,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): bool {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

return static::existsByQueryBuilder($qb);
return static::existsByQueryBuilder(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}
63 changes: 52 additions & 11 deletions src/Resources/views/Generator/ColumnTemplate.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

public static function filterBy{{ column }}(QueryBuilder $qb, $value, $operator = Comparison::EQ, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterBy{{ column }}(
QueryBuilder $qb,
$value,
$operator = Comparison::EQ,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
if ($value === null) {
if (Comparison::NEQ === $operator) {
$qb->andWhere($entityName.'.'.$columnName.' IS NOT NULL');
Expand All @@ -19,8 +24,12 @@
return $qb;
}

public static function filterIn{{ column }}(QueryBuilder $qb, $value, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterIn{{ column }}(
QueryBuilder $qb,
$value,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
if ($value === null) {
$qb->andWhere($entityName.'.'.$columnName.' IS NULL');
} else {
Expand All @@ -34,8 +43,12 @@
return $qb;
}

public static function filterNotIn{{ column }}(QueryBuilder $qb, $value, $entityName = '{{ entityDql }}', $columnName = '{{ columnDql }}'): QueryBuilder
{
public static function filterNotIn{{ column }}(
QueryBuilder $qb,
$value,
$entityName = '{{ entityDql }}',
$columnName = '{{ columnDql }}',
): QueryBuilder {
if ($value === null) {
$qb->andWhere($entityName.'.'.$columnName.' IS NOT NULL');
} else {
Expand All @@ -50,36 +63,64 @@
return $qb;
}

public function findBy{{ column }}(mixed $value): array
{
public function findBy{{ column }}(
mixed $value,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): array {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

return static::getQueryBuilderResult($qb);
return $this->getQueryBuilderResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

public function findOneBy{{ column }}(
mixed $value,
bool $allowNull = false,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): ?\{{ entityClasspath }} {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

if ($allowNull) {
return static::getQueryBuilderOneOrNullResult($qb);
return $this->getQueryBuilderOneOrNullResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

return static::getQueryBuilderSingleResult($qb);
return $this->getQueryBuilderSingleResult(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

public function existsBy{{ column }}(
mixed $value,
bool $useQueryCache = false,
?string $cacheId = null,
array $resultCacheTags = [],
): bool {
$qb = $this->getNewQueryBuilder();
static::filterBy{{ column }}($qb, $value);

return static::existsByQueryBuilder(
qb: $qb,
useQueryCache: $useQueryCache,
cacheId: $cacheId,
resultCacheTags: $resultCacheTags,
);
}

Expand Down
Loading

0 comments on commit f69d784

Please sign in to comment.