Contents
- Basic Concepts
- Models and Schemas
- Column Types
- Collections
- Filtering
- Groups
- Inverting Filters (not)
- Back references
- Advanced Filters
- Creating Your Own Filter
- Default Filters
- Relationships
- Data Decorators
- Data Storage and Typing
- Repositories
- Data Typing and Data Transforms
- Advanced Techniques
- Joins and Intersections
- Aggregates
- Model Events
- Custard Commands
- Updating DocBlock comments
- Migrating Schemas
- Common Patterns and Anti-Patterns
- Inter-Model Strategies
Value back references
Sometimes instead of filtering using literal values you want to filter using the value of other columns in a model. Stem supports this by way of a placeholder syntax:
@{ColumnName}
Simply use this in place of a literal value to have this used instead.
For example to select customer records where their outstanding balance is greater than their credit limit you could use a filter like this:
$overLimit = Customer::find(new GreaterThan("OutstandingBalance", "@{CreditLimit}"));
This occurs most frequently when performing intersections, pulling up an aggregate value and then filter. In the above example we supposed that the OutstandingBalance was calculated into a simple column on the customer model. What if it wasn't and had to be calculated on the fly:
$overLimit = Customer::all()
->intersectWith(
Invoice::all()
// Aggregate the balance of invoices for the customer
->addAggregateColumn(new Sum("OutstandingBalance")),
"CustomerID",
"CustomerID",
// Pull up the aggregate value into the customer collection
[ "SumOfOutstandingBalance" ])
// Filter on the customer collection back referencing the aggregate value.
->filter(new LessThan("CreditLimit", "@{SumOfOutstandingBalance}"));