Search code examples
phpmysqldoctrine-ormdqlquery-builder

Doctrine 2 DQL - nest expressions and functions in the QueryBuilder


I have found DoctrineExtensions module that has implement some useful MySQL functions. But I think my problem is a general one. How would you pass another mysql expression as a parameter into a function?

If I had 2 columns updated_at which is a DATETIME and lifespan which is an INT and I wanted to query whether the lifetime of an entity had expired, I could do this;

WHERE 0 > TIMESTAMPDIFF(SECONDS, NOW(), e.updated_at + INTERVAL e.lifespan SECOND)

IN the QueryBuilder it's something like this.

$qb->andWhere('0 > TIMESTAMPDIFF(SECONDS, CURRENT_TIMESTAMP(), e.updated_at + INTERVAL e.lifespan SECOND)');

But the parser doesn't like + INTERVAL e.lifespan SECOND because it expects the Close Parenthesis instead of a plus sign.


Solution

  • I realized I can use the DateAdd class from the DoctrineExtensions module.

    TIMESTAMPDIFF(SECOND, CURRENT_TIMESTAMP(), DATEADD(e.updated_at, INTERVAL e.lifespan SECOND))