Download the PHP package cryonighter/formula-doctrine-bundle without Composer
On this page you can find all versions of the php package cryonighter/formula-doctrine-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cryonighter/formula-doctrine-bundle
More information about cryonighter/formula-doctrine-bundle
Files in cryonighter/formula-doctrine-bundle
Package formula-doctrine-bundle
Short Description Symfony bundle integrating Hibernate-style #[Formula] computed fields into Doctrine ORM entities
License MIT
Homepage https://github.com/cryonighter/formula-doctrine-bundle
Informations about the package formula-doctrine-bundle
Formula Doctrine Bundle
Symfony bundle for integrating cryonighter/formula-doctrine
into Symfony applications.
It enables Hibernate-style #[Formula] computed fields for Doctrine ORM entities
and wires the required Doctrine metadata listeners, SQL walker configuration and
DBAL middleware automatically through Symfony's dependency injection container.
Use it when you want read-only entity properties whose values are computed by DQL/SQL expressions, subqueries, aggregations or joins — without adding physical database columns and without introducing N+1 queries.
Example with native SQL subquery – must be enclosed in parentheses:
Example using DQL subquery – should not be enclosed in parentheses:
With this bundle installed, formula fields are populated automatically when entities
are loaded through Doctrine in a Symfony application. The bundle keeps your entity
code focused on the #[Formula] attributes while taking care of registering the
integration services needed by cryonighter/formula-doctrine.
Requirements
- PHP >= 8.2.0 but the latest stable version of PHP is recommended
Install
Via Composer
The bundle will be automatically registered in config/bundles.php:
Bundle Registration Order
If you use other bundles that extend Doctrine ORM with custom SQL walkers
(e.g. Gedmo DoctrineExtensions, API Platform), register FormulaDoctrineBundle
last in config/bundles.php:
FormulaDoctrineBundle automatically detects and chains with any previously
registered output walker, so both transformations are applied to every query.
If another bundle is registered after FormulaDoctrineBundle and also sets a
custom output walker globally, you may need to manually call
FormulaDoctrineConfigurator::configure() in your application's bundle.
Usage
Basic example
Add #[Formula] to any property on a Doctrine entity.
The property must not be mapped with #[ORM\Column].
Fetching entities
No changes to your query code are needed.
Formula fields are populated automatically on every DQL SELECT:
A single SQL query is executed — no N+1:
QueryBuilder
Works with QueryBuilder too:
And in the repositories too:
Methods find(), findBy(), findOneBy() and findAll() are also supported:
Using formula fields in queries
Formula fields can be used in WHERE, ORDER BY, GROUP BY and HAVING clauses
just like regular entity properties:
WHERE clause
Filter entities by computed values:
ORDER BY clause
Sort by formula fields:
GROUP BY and HAVING clauses
Aggregate and filter by computed values:
Combined example
All clauses together in a single query:
Note: Formula fields work transparently in all query clauses. The SQL subquery is embedded only once per query, not per clause usage.
Aggregate functions
All DQL aggregate functions (e.g. COUNT, SUM, AVG, MIN, MAX) work with formula fields out of the box:
Note:
MINandMAXignoreNULLvalues — so nullable formula fields (e.g.?float $maxOrderTotal) behave correctly even when some entities have no related records.
CASE WHEN expressions
Formula fields can be used inside CASE WHEN ... THEN ... END expressions
directly in DQL — for categorisation, conditional sorting and custom labels:
Nullable fields
If a formula can return NULL (e.g. MAX on an empty set),
declare the property as nullable — the type is inferred automatically:
The {this} placeholder
Use {this} to reference the root entity's table alias in the native SQL expression or root entity itself in the DQL expression.
In native SQL, {this} is resolved to the actual Doctrine-generated table alias (e.g. c0_):
In DQL, {this} refers to the root entity itself, so you compare against the entity
reference directly — without a field suffix:
Do not hardcode the table name or alias directly — it will break when Doctrine generates a different alias.
Custom SELECT alias
By default the SQL column alias matches the property name.
Override it with the alias parameter:
Use a custom alias only when you need to control the raw SQL column name, e.g. for compatibility with a specific reporting tool.
Nested Formulas
A #[Formula] expression can reference a formula field of another entity.
The entire chain is resolved into a single SQL query.
Note: In a native SQL expression, reference another formula field by its
aliasif one is declared (e.g.c.total), or by the property name otherwise. In a DQL expression, always use the property name (e.g.c.orderCount).
UPDATE queries
Formula fields can be used in the WHERE clause of DQL UPDATE queries —
filter which entities to update based on computed values:
Note: Formula fields are read-only and are never written to the database. They can only appear in
WHEREclauses ofUPDATE/DELETE— not in theSETclause.
DELETE queries
Formula fields work identically in DQL DELETE queries:
How it works
You can read about this in the description of the base package cryonighter/formula-doctrine.
Limitations
| Limitation | Notes |
|---|---|
| Read-only fields | Formula fields must not have #[ORM\Column]. They are registered internally by the library and must never be written to the database. |
| Scalar types only | Supported PHP types: int, float, string, bool, \DateTime, \DateTimeImmutable, \DateTimeInterface and their nullable variants. Always provide a default value for non-nullable formula properties (e.g. public int $orderCount = 0). |
| Native SQL | $em->getConnection()->executeQuery(...) bypasses both Walker and Middleware entirely — formula fields will hold their default PHP values. |
| Schema Tool | doctrine:schema:create and doctrine:schema:update do not create columns for formula fields — they have no physical column in the database. This is correct behaviour. |
| Walker Chaining order | FormulaDoctrineBundle must be registered last in config/bundles.php among Doctrine-extending bundles to ensure correct Walker Chaining. See Bundle Registration Order. |
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of formula-doctrine-bundle with dependencies
cryonighter/formula-doctrine Version ^1.4.0
symfony/dependency-injection Version >=6.4
symfony/http-kernel Version >=6.4