Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine query builder ~ datetime

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?

like image 357
Nick Avatar asked Aug 12 '14 18:08

Nick


People also ask

Is Query builder an ORM?

The ORM's query builder provides a simple to use fluent interface for creating and running queries.

How does query builder work?

Query Builder is designed to enhance productivity and simplify SQL query building tasks. Query Builder provides a graphical user interface for creating SQL queries. You can drag-and-drop multiple tables, views and their columns onto a visual designer to generate SQL statements.

What is Query builder in C#?

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird. sqlkata.com.

What is Query builder in Java?

The query builder is a utility to generate CQL queries programmatically.


1 Answers

m.postdate is a field name and therefore shouldn't be passed in as a parameter. Try this

$qb = $em->createQueryBuilder();
$qb->select('m')
                 ->from('ChatboxBundle:ChatMessage', 'm')
                 ->where('m.postdate > :last')
                 ->setParameter('last', new \DateTime('-5 second'), \Doctrine\DBAL\Types\Type::DATETIME);
$updatedata = $qb->getQuery()->getResult();
like image 87
FuzzyTree Avatar answered Sep 28 '22 12:09

FuzzyTree