Skip to content

Conversation

reclikp
Copy link

@reclikp reclikp commented Mar 12, 2025

Hi, I just wanted to add a limit function that can be useful for nested queries. If we just try to use setMaxResults(1) in a QueryBuilder or LIMIT 1 at the end of a subquery, it will throw errors.

The function is currently available for MySQL, PostgreSQL and SQLite.

Usage of the function:

LIMIT((subquery), limit)

Example:

$subQb = $this->getEntityManager()->createQueryBuilder()
    ->select("category")
    ->from("App\Entity\Category", "category")
    ->orderBy("category.id", "DESC"));
    
$qb = $this->getEntityManager()->createQueryBuilder()
    ->select("expense")
    ->from("App\Entity\Expense", "expense")
    ->andWhere("expense.category = LIMIT((${subQb->getDQL()}), 1)");

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

Successfully merging this pull request may close these issues.

1 participant