Download the PHP package ucscode/doctrine-expression without Composer
On this page you can find all versions of the php package ucscode/doctrine-expression. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ucscode/doctrine-expression
More information about ucscode/doctrine-expression
Files in ucscode/doctrine-expression
Package doctrine-expression
Short Description A PHP class for handling SQL queries across different database platforms using Doctrine.
License MIT
Informations about the package doctrine-expression
DoctrineExpression
DoctrineExpression
is a PHP library designed to facilitate cross-database compatibility when constructing SQL queries using Doctrine ORM. By abstracting database-specific syntax, developers can write more maintainable code that seamlessly adapts to different database drivers.
Table of Contents
- Features
- Requirements
- Installation
- Usage
- License
- Contributing
- Acknowledgments
Features
- Write database-agnostic queries that adapt to MySQL, PostgreSQL, SQLite, and more.
- Define SQL queries for each supported database platform using simple callable functions.
- Easily integrates into your existing Doctrine setup.
Requirements
- PHP 8.2 or higher
- Composer
Installation
To install Doctrine\Expression
, run the following command in your terminal:
Usage
Scenario: Changing Database Drivers
Consider a scenario where your application is initially configured to use MySQL. The following configuration might be present in your Symfony .env
file:
You might have a query that leverages JSON capabilities specific to MySQL:
This query works well with MySQL. However, if you decide to switch to PostgreSQL, you might change your .env
configuration to:
Adapting the Query for PostgreSQL
Switching to PostgreSQL requires some modifications due to syntax differences. For example, PostgreSQL handles JSON operations differently:
Leveraging DoctrineExpression
Instead of rewriting queries for each database driver, you can utilize Doctrine\Expression
to manage this complexity efficiently.
Example Usage:
The getCompatibleResult()
method checks which platform is active (e.g., MySQL or PostgreSQL) and selects the corresponding query that was previously defined using defineQuery()
.
Providing extra Parameters to the query
You might want a set of data to be available when defining your query in a closure. For that, you can pass an array with the parameter as second argument of the Doctrine\Expression
or you can call the Doctrine\Expression::set()
method
Example:
For drivers that might use the same syntax, the getDefinedQuery()
can be used to eliminate the need to write repetitive patterns.
Conclusion
By using Doctrine\Expression
, you can maintain a clean and consistent codebase while easily adapting to changes in database platforms. This library not only saves time but also enhances collaboration among developers familiar with different SQL dialects.
Helpful Snippet for Common Use Case
How to find users by one or more roles in Symfony using Doctrine\Expression
Note:
You can handle PostgreSQL JSON/JSONB using libraries like one of the following:
- https://github.com/martin-georgiev/postgresql-for-doctrine
- https://github.com/ScientaNL/DoctrineJsonFunctions
The snippet below does not employ any external libraries and works without doctrine extension dependencies but only for this simple case.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Fork and send a pull request
Acknowledgments
- Doctrine ORM for providing a powerful and flexible object-relational mapping layer for PHP.