Home:ALL Converter>Doctrine query builder ~ datetime

Doctrine query builder ~ datetime

Ask Time:2014-08-13T02:39:17         Author:Nick

Json Formatter

I'm trying to create a simple chatbox in symfony2 / doctrine 2.

For testing I'm checking for new messages every 5 seconds, so in my query I try to get all messages by getting all messages with a datetime greater than the current one minus 5 seconds.

I try to do so the following way, but it returns all messages in the database instead of the ones posted in the last 5 seconds

$em = $this->getDoctrine()->getManager();

$qb = $em->createQueryBuilder();
$qb->select('m')
                 ->from('ChatboxBundle:ChatMessage', 'm')
                 ->where(':new > :last')
                 ->setParameter('new', 'm.postdate' )
                 ->setParameter('last', new \DateTime('-5 second'), \Doctrine\DBAL\Types\Type::DATETIME);
$updatedata = $qb->getQuery()->getResult();

Any ideas on what I'm doing wrong?

Author:Nick,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25271726/doctrine-query-builder-datetime
FuzzyTree :

m.postdate is a field name and therefore shouldn't be passed in as a parameter. Try this\n\n$qb = $em->createQueryBuilder();\n$qb->select('m')\n ->from('ChatboxBundle:ChatMessage', 'm')\n ->where('m.postdate > :last')\n ->setParameter('last', new \\DateTime('-5 second'), \\Doctrine\\DBAL\\Types\\Type::DATETIME);\n$updatedata = $qb->getQuery()->getResult();\n",
2014-08-12T18:44:32
yy