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?
The ORM's query builder provides a simple to use fluent interface for creating and running queries.
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.
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird. sqlkata.com.
The query builder is a utility to generate CQL queries programmatically.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With