Format Where Clause with comparison interval in ORM

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    Format Where Clause with comparison interval in ORM

    Hello,

    The following code does not work. How do I create this query using ORM syntax ?

    PHP Code:
    ->where([
    'evaluationDate>' => 'DATE_SUB(NOW(),INTERVAL 11 YEAR)',
    ])
    Currently, this query gives me the following error:

    ERROR: (HY000) SQLSTATE[HY000]: General error: 1525 Incorrect DATETIME value: 'DATE_SUB(NOW(),INTERVAL 11 YEAR))'

    This is odd to me since the following code works directly in mysql

    Code:
    SELECT
    *
    FROM
    `equipment_performance_evaluation_report`
    WHERE
    `evaluation_date` > DATE_SUB(NOW(), INTERVAL 11 YEAR)​
  • yuri
    Member
    • Mar 2014
    • 8453

    #2
    Advice, do it in PHP, pass a result string-date.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • yuri
      yuri commented
      Editing a comment
      'evaluationDate>' => (new \DateTimeImmutable)->modify('-11 years')->format('Y-m-d H:i:s'),
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    #3
    Thanks for the suggestion. I thought maybe something clever like complex expression TIMESTAMPDIFF_YEAR could be useful and tried building Expression with it but Expression::TIMESTAMPDIFF_YEAR does not exist. I didn't know about DateTimeImmutable but that is useful. I ended up just doing it in PHP the old-fashioned way.

    PHP Code:
    $retentionDate = date("Y-m-d", strtotime("-11 years")); 
    
    PHP Code:
    ->where([
    'evaluationDate>' => $retentionDate,
    ])

    Comment

    Working...