Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Formula Doctrine

Formula Doctrine Laravel Package

cryonighter/formula-doctrine

Hibernate-style #[Formula] computed fields for Doctrine ORM 3 entities. Define read-only SQL or DQL expressions (subqueries, aggregates, joins) as properties and have them hydrated automatically in one query—avoiding N+1. Supports Symfony bundle or standalone setup.

View on GitHub
Deep Wiki
Context7

Hibernate-style attribute #[Formula] computed fields for Doctrine ORM entities

Frequently asked questions about Formula Doctrine
Can I use cryonighter/formula-doctrine in a Laravel project without Symfony?
Yes, but you’ll need to manually bootstrap the package by registering listeners, middleware, and configurators when setting up your Doctrine EntityManager. The package provides standalone installation instructions for non-Symfony environments, including Laravel. A Laravel-specific service provider could abstract this setup for easier integration.
Will this package work with Laravel’s Eloquent ORM?
No, this package is designed for Doctrine ORM entities. If you’re using Eloquent exclusively, you’ll need to either switch to Doctrine for formula fields or explore alternatives like custom accessors or query scopes. Hybrid setups (Eloquent for CRUD, Doctrine for formulas) are possible but require careful configuration.
How do I handle database-specific SQL syntax (e.g., PostgreSQL vs. MySQL) in formulas?
The package uses placeholders like `{this}` to avoid SQL injection and supports any DBAL-compatible database. However, you’ll need to adjust SQL syntax (e.g., `COALESCE`, `LIMIT`, or window functions) manually for non-PostgreSQL dialects. Test formulas thoroughly across your target databases to ensure compatibility.
Does this package support Laravel’s caching (Redis, file cache) for formula fields?
Formula fields are computed per query and may not integrate seamlessly with Laravel’s caching layers. Doctrine’s result cache or second-level cache might not work as expected for dynamic subqueries. For caching derived data, consider precomputing values in a separate table or using Laravel’s cache with a TTL.
Are there performance risks with complex subqueries in formulas?
Yes, overly complex subqueries can degrade query performance, especially on large datasets. Test formulas with `EXPLAIN ANALYZE` to identify bottlenecks. Avoid deep nesting or expensive joins in formulas. For analytics, consider materialized views or pre-aggregated tables instead.
How do I debug or validate formula syntax before runtime?
The package doesn’t include built-in syntax validation, but you can manually test formulas by inspecting generated SQL (enable Doctrine’s SQL logging). For DQL, validate against your entity structure. Consider writing unit tests with mock queries or using a CLI tool like `doctrine:schema:validate` to catch issues early.
Can I use #[Formula] with Laravel migrations or schema updates?
Formula fields don’t affect your database schema, so they won’t interfere with migrations. However, if your formulas reference tables/columns that change (e.g., renamed in a migration), you’ll need to update the formula strings manually. The package doesn’t auto-detect schema changes.
Is there a Laravel-specific bundle or wrapper for this package?
Not yet, but the package is designed for standalone use. You could create a Laravel service provider to auto-register Doctrine listeners and middleware. Alternatively, check for community packages like `spatie/laravel-doctrine-orm` that might bridge Laravel and Doctrine integration gaps.
How do I handle user input in formulas safely?
The package uses placeholders like `{this}` to bind entity properties, preventing SQL injection. Avoid dynamically interpolating raw user input into formula strings. For dynamic conditions, use Doctrine’s `ExpressionBuilder` or parameter binding instead of string concatenation.
What are the alternatives to #[Formula] for computed fields in Laravel?
For Eloquent, consider custom accessors (`getOrderCountAttribute()`) or query scopes. For Doctrine, alternatives include computed columns (database-level), materialized views, or precomputing values in application logic. Packages like `stancl/tenancy` or `spatie/laravel-activitylog` handle derived data but with different trade-offs.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle