Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "when()" method to QueryBuilder to be more readable conditional query #11350

Open
iku12phycho opened this issue Mar 12, 2024 · 0 comments
Open

Comments

@iku12phycho
Copy link

Feature Request

Hello,
Code visibility is good if Query Builder is more Declarative.

Q A
New Feature yes
RFC no
BC Break no

Summary

add when()method to contain condition statements.
nests and if statements are less.

when()method has follow arguments

  • $condition (boolean)
  • $truthy (Closure) run if $condition is true
  • $falsy(Closure|null) run if $condition is false. when it is null, skip.

before

// switch null clause
if ($label === null) {
  $queryBuilder->where(`label is :label`)
    ->setParameter( 'label', $label);
}else {
  $queryBuilder->where(`label = :label`)
    ->setParameter( 'label', $label);
}
// conditional clause
if (!$isAdmin) {
  $queryBuilder->addWhere('isPublic = true');
}

after

$queryBuilder->when(
  condition: $label === null,
  truthy: fn ($queryBuilder) => $queryBuilder->where(`label is :label`),
  falsy: fn ($queryBuilder) => $queryBuilder->where(`label = :label`)
)
->setParameter( 'label', $label)
->when(!$isAdmin, fn ($queryBuilder) => $queryBuilder->addWhere('isPublic = true'))
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant