Download the PHP package rgalstyan/symfony-aggregated-queries without Composer

On this page you can find all versions of the php package rgalstyan/symfony-aggregated-queries. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package symfony-aggregated-queries

Symfony Aggregated Queries

Latest Version Total Downloads Tests License

Reduce multi-relation Doctrine queries to a single optimized SQL statement using JSON aggregation.

Solves Doctrine's documented N+1 problem (Issue #4762) where fetch="EAGER" still generates multiple queries for OneToMany/ManyToMany relations.

Perfect for read-heavy APIs, dashboards, and admin panels where traditional Doctrine eager loading generates too many queries.


πŸ”₯ The Problem

When querying Doctrine entities, relations are loaded either lazily (classic N+1) or eagerly using separate queries for collections.

Example with traditional Doctrine entity loading:

Still produces 3–4 separate queries:

While this is not a fetch join, it still results in multiple database round-trips, heavy Doctrine hydration, increased memory usage, and slower response times.

Fetch joins can reduce the number of queries to one, but introduce row explosion, break pagination, and do not scale when multiple collections are involved.

Doctrine's Known Issue:
Even with fetch="EAGER", OneToMany and ManyToMany relations cause N+1 queries. This is a documented limitation that has existed since 2015.

Complex pages easily generate 5–15 queries, increasing:


✨ The Solution

Transform multiple queries into one optimized SQL statement using JSON aggregation:

Generates a single query:

Result:


πŸ“Š Performance

Real-world benchmark using a Symfony application with a realistic e-commerce dataset (products, images, reviews).

Dataset context:

Example run (limit = 1000):

Method Time Memory Queries
Traditional Eloquent / Doctrine-style loading 167.49ms 24.37 MB 23
Aggregated Query 28.17ms 6.91 MB 1
Improvement ⚑ 83.2% faster πŸ’Ύ 71.6% less πŸ”’ 22 fewer

These numbers depend on hardware and DB state, but the overall trend is consistent. The primary gains come from reduced SQL round-trips and avoiding ORM entity hydration.

Reproducible benchmark project

A standalone Symfony-based benchmark with full setup instructions and fixtures is available here:

The benchmark focuses on read-only, DTO-style queries and compares traditional ORM loading versus aggregated SQL queries in real-world conditions.

πŸ“‹ Requirements

Component Version
PHP ^8.1
Symfony ^6.0 | ^7.0
Doctrine ORM ^2.14 | ^3.0
MySQL ^8.0
PostgreSQL ^12.0

πŸ“¦ Installation

1. Install via Composer

2. Enable the Bundle

If you're using Symfony Flex, the bundle is automatically registered.

Otherwise, add to config/bundles.php:

3. (Optional) Configure

Create config/packages/aggregated_queries.yaml:


πŸš€ Quick Start

1. Add trait to your repository

2. Define your entities

3. Use in your service

4. Use in controller

5. Response structure (guaranteed)


πŸ’‘ Advanced Usage

Direct Service Usage

Inject AggregatedQueryBuilder directly when you need more flexibility:

Filtering and Sorting

Collections (OneToMany)

Multiple Counts


πŸ“– API Reference

Loading Relations

Query Filters

Execution

Debugging


βœ… When to Use

Perfect for:

⚠️ Not suitable for:


πŸ”’ Important Constraints

Read-Only by Design

Results are arrays, not Doctrine entities (by default).

This means:

Use for read operations only. For writes, use standard Doctrine.

Data Shape Guarantees

Feature Always Returns
withJsonRelation() array or null
withJsonCollection() array (empty [] if no records)
withCount() integer

No surprises. No null collections. Consistent types across MySQL and PostgreSQL.


πŸ“¦ Batch Processing

For large exports or background jobs:

Do NOT use limit(5000) without batching!


βš™οΈ Configuration Reference


⚠️ Limitations (v1.0)

Currently not supported (planned for future versions):

See Roadmap for planned features.


πŸ†š Comparison: Traditional vs Aggregated

Traditional Doctrine

Aggregated Queries


πŸ§ͺ Testing


πŸ”§ Troubleshooting

"AggregatedQueryBuilder not injected"

Cause: Repository not configured as a service with auto-wiring.

Solution: Ensure repositories are in services.yaml:

"Relation 'xyz' not found"

Cause: Typo in relation name or relation not defined in entity.

Solution: Check entity's #[ORM\ManyToOne], #[ORM\OneToMany], etc. attributes.

Slow performance

Causes:

Solutions:


πŸ“š Examples

See the /examples directory for complete working examples:

Example Description
basic-usage.php Simple queries with 2-3 relations
multiple-relations.php Complex relations and collections
with-filters.php Filtering, sorting, and pagination
service-usage.php Service layer integration
batch-processing.php Large dataset handling

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Add tests for new features
  4. Ensure tests pass (composer test)
  5. Check code style (composer cs:check)
  6. Run static analysis (composer phpstan)
  7. Commit your changes (git commit -m 'Add amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. Open a Pull Request

See CONTRIBUTING.md for detailed guidelines.


πŸ” Security

If you discover a security vulnerability, please email:

πŸ“§ [email protected]

Do not create public issues for security vulnerabilities.

All security vulnerabilities will be promptly addressed.


πŸ“ Changelog

See CHANGELOG.md for release history and migration guides.


πŸ—ΊοΈ Roadmap

v1.1 (Q1 2025)

v2.0 (Q2-Q3 2025)

Want a feature? Open an issue or vote on existing ones!


πŸ“„ License

The MIT License (MIT). See LICENSE for details.


πŸ‘¨β€πŸ’» Credits

Author: Razmik Galstyan
GitHub: @rgalstyan
Email: [email protected]
LinkedIn: Razmik Galstyan

Inspired by Laravel Aggregated Queries and built to solve Doctrine's N+1 problem.

Built with ❀️ for the Symfony community.


πŸ”— Related Projects


πŸ’¬ Support


🎯 Why This Bundle Exists

Doctrine's ORM is powerful but has a well-known N+1 problem with collections that fetch="EAGER" doesn't solve (documented since 2015).

This bundle provides a clean, performant solution using modern SQL's JSON aggregation capabilities:

Perfect for: APIs, dashboards, mobile backends, and any read-heavy Symfony application.


🌟 Show Your Support

If this bundle saves you time and improves your app's performance, please:

Every star and contribution helps the project grow!


**[πŸš€ Quick Start](#quick-start)** β€’ **[πŸ’‘ Examples](examples/)** β€’ **[πŸ› Issues](https://github.com/rgalstyan/symfony-aggregated-queries/issues)** β€’ **[πŸ’¬ Discussions](https://github.com/rgalstyan/symfony-aggregated-queries/discussions)** Made with ❀️ by [Razmik Galstyan](https://github.com/rgalstyan)

All versions of symfony-aggregated-queries with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
doctrine/dbal Version ^3.6 || ^4.0
doctrine/orm Version ^2.14 || ^3.0
symfony/config Version ^6.0 || ^7.0
symfony/dependency-injection Version ^6.0 || ^7.0
symfony/http-kernel Version ^6.0 || ^7.0
symfony/yaml Version ^6.0 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rgalstyan/symfony-aggregated-queries contains the following files

Loading the files please wait ...