Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add operators in doctrine2 query builder where statement

Im trying to select from a table where the selected duration divided by the entry duration equals has no remainder.

$qb = $em->createQueryBuilder()
->from('AcmeBlogBundle:Entry', 'e')
->andWhere(":duration % e.duration = 0")
->setParameter('duration', $duration);

This returns the error:

[Syntax Error] line 0, col 226: Error: Expected =, <, <=, <>, >, >=, !=, got '%' 

This would work in plain SQL. Does anybody know how to do this with Doctrine's query builder?

like image 873
Apothan Avatar asked Jun 14 '12 20:06

Apothan


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?

Using Query Builder, you can search and filter database objects, select objects and columns, create relationships between objects, view formatted query results, and save queries with little or no SQL knowledge.

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 doctrine SQL?

Doctrine features a powerful query builder for the SQL language. This QueryBuilder object has methods to add parts to an SQL statement. If you built the complete state you can execute it using the connection it was generated from. The API is roughly the same as that of the DQL Query Builder.


1 Answers

The symbol % is not a DQL operator. Try this:

$qb = $em->createQueryBuilder()
->from('AcmeBlogBundle:Entry', 'e')
->andWhere("mod(:duration,e.duration) = 0")
->setParameter('duration', $duration);

Or read this: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/dql-doctrine-query-language.html The 12.5.1 paragraph.

MOD(a, b) - Return a MOD b.
like image 154
sensorario Avatar answered Oct 06 '22 01:10

sensorario