2

I try to make this query with doctrine 1.2:

 $q->where('date > ?', 
             new Doctrine_Expression('DATE_SUB(CURDATE() , INTERVAL 7 DAY)'));

but it's not return me any results.

any idea ?

thanks

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • I assume you have checked all this, but for completeness' sake - is `date` a DATETIME column? Do you have data that matches the criteria? – Pekka Oct 06 '11 at 08:59
  • yes i check this, its work like $q->where('date > ?','2011-10-04') – Haim Evgi Oct 06 '11 at 09:06

1 Answers1

5

The reason why it doesn't return anything is because Doctrine escapes the expression - the generated SQL is

WHERE (date > 'DATE_SUB(CURDATE(), INTERVAL 7 DAY)')

rather than

WHERE (l.action_time > DATE_SUB(CURDATE(), INTERVAL 7 DAY))

You could force it to work like this:

$date = new Doctrine_Expression('DATE_SUB(CURDATE() , INTERVAL 7 DAY)');
$q->where('date > ' . $date);

This isn't the safest option however, as the input doesn't get escaped and isn't good practice...

mogoman
  • 2,286
  • 24
  • 28
  • I am using Doctrine2 with Symfony2, I tried your above suggestion but it says \Doctrine_Expression' not found in. Can you tell me what and how can i use Doctrine_Expression in my repository – ScoRpion Apr 06 '12 at 09:09
  • This question is about Doctrine 1.2. Doctrine 2 is a completely different product. – Pelle Mar 07 '13 at 12:38