Skip to content

Use of "Between" to check whether two Entity properties contain a specific date #13

@holtkamp

Description

@holtkamp

While trying to rewrite some filtering functionality with Specifications, I think I encountered a limitation when trying to determine whether an Entity is 'valid' at a specific DateTime.

The Entity has two properties of type DateTime:

  • validFrom: DateTime when Entity becomes valid
  • validTo: DateTime when Entity is no longer valid

Using the ExpressionBuilder:

$expressionBuilder->between(':specificDateParameter', 'x.validFrom', 'x.validTo');

Using Specifications:

new Specification([
    new Between($entityPropertyName, $fromParameter, $toParameter),
]);

So

  • the ExpressionBuilder checks whether a specific date is between a range defined by Entity properties
    • one parameter used
    • two Entity properties involved
  • the Specification checks whether a specific Entity property is between a certain range
    • two parameters used
    • one Entity property involved

One could use:

new Specification([
    new GreaterThanOrEquals('validFrom', $specificDate),
    new LessThanOrEquals('validTo', $specificDate),
]);

But I kind of 'prefer' the elegance of Between...
Any idea / pointers on how to deal with this scenario?

Additionally
And what if the year should not be taken into account? We used DayOfYear() with the ExpressionBuilder:

$expressionBuilder->between(
    'DayOfYear(:specificDateParameter)', 
    'DayOfYear(x.validFrom)', 
    'DayOfYear(x.validTo)'
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions