Questions tagged [dql]

Doctrine Query Language (DQL) is an Object Query Language created for helping users in complex object retrieval.

Summary

DQL stands for Doctrine Query Language and is an Object Query Language derivate that is very similar to the Hibernate Query Language (HQL) or the Java Persistence Query Language (JPQL).

In essence, DQL provides powerful querying capabilities over your object model. Imagine all your objects lying around in some storage (like an object database). When writing DQL queries, think about querying that storage to pick a certain subset of your objects.

Questions

  • How to write DQL statements to achieve a particular SQL statement?
  • Why will a DQL statement not work as expected?

More Information

To learn more about DQL visit

Doctrine 1.2
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en#dql-doctrine-query-language

Doctrine 2.0
http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html

1296 questions
23
votes
5 answers

Doctrine 2 DQL - how to select inverse side of unidirectional many-to-many query?

I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to). How…
Gnuffo1
  • 3,478
  • 11
  • 39
  • 53
22
votes
1 answer

Symfony 2 / Doctrine user-defined DQL functions parameter have string length limit?

I created my custom DQL function for Doctrine DQL: class Translate extends FunctionNode { public $field; public function getSql(SqlWalker $sqlWalker) { $query = "TRANSLATE(" . $this->field->dispatch($sqlWalker) . ",…
Seb33300
  • 7,464
  • 2
  • 40
  • 57
21
votes
5 answers

Doctrine DQL, class table inheritance and access to subclass fields

I have a problem with a DQL query and entity specialization. I have an Entity called Auction, which is OneToOne relation with Item. Item is a mappedSuperclass for Film and Book. I need a query that could back a search engine, allowing the user to…
crizzis
  • 9,978
  • 2
  • 28
  • 47
20
votes
5 answers

Doctrine 2: Query result as associative array

In my Repository class I use the following code to query: $query = $this->getEntityManager()->createQuery(" SELECT s.term, COUNT(s.term) AS freq FROM App\Entities\SearchTerm s GROUP BY s.term ORDER BY s.term ASC "); $result =…
Yeroon
  • 3,223
  • 2
  • 22
  • 29
19
votes
2 answers

How to order by a computed value in DQL

I'm trying to order the results of my query by whether or not they match my original entity on a property. I could do this easily in mySQL with the following query: SELECT * FROM table ORDER BY prop = 'value' DESC; However, in Doctrine, when I…
SnailCoil
  • 818
  • 1
  • 9
  • 23
17
votes
2 answers

Deleting record in many-to-many table

I'm following the security chapter of the Symfony 2 book. There's is an example with a table USERS and GROUPS. There is a many-to-many relationship between USERS and GROUPS, which creates in the database a table called USERGROUPS. What I want is to…
Francisco Ochoa
  • 1,538
  • 4
  • 19
  • 43
17
votes
1 answer

Doctrine 2.1 DQL - Many-to-many query multiple values - Item in multiple categories?

This may seem like a rudimentary request, but I can't seem to get it to work, so I'm either missing something stupid, or am not understanding how it should be done. Thanks in advance. I have two doctrine entities with a many-to-many relationship:…
Ian Fosbery
  • 747
  • 1
  • 5
  • 11
17
votes
4 answers

Doctrine Query Builder not working with UPDATE and INNER JOIN

In my repository I have this query: $qb = $this->getEntityManager()->createQueryBuilder(); $qb ->update('MyBundle:Entity1', 'e1') ->join('e1.Entity2', 'e2') ->set('e1.visibile', '1') ->andWhere('e2.id = :id')->setParameter("id",…
Alessandro Pessina
  • 243
  • 1
  • 3
  • 8
17
votes
1 answer

doctrine2 - how to use DATE_ADD function

I am trying to use the DATE_ADD function from doctrine2 but I am having trouble get it right. I am using like this in DQL: ->andWhere('p.created_at <= DATE_ADD(CURRENT_DATE(),4, day)') but I am getting syntax error: [Syntax Error] line 0, col 215:…
brpaz
  • 3,618
  • 9
  • 48
  • 75
16
votes
5 answers

How to do a LIKE database query in Symfony2

This should be simple but I can't find a working example. Here's a controller method that throws the error "Invalid parameter number: number of bound variables does not match number of tokens". I'm posting the "searchterm" variable successfully…
Acyra
  • 15,864
  • 15
  • 46
  • 53
16
votes
3 answers

How to find by referenced document in Doctrine ODM with MongoDB?

I have one document in my "params" collection like this: { "_id": ObjectId("4d124cef3ffcf6f410000037"), "code": "color", "productTypes": [ { "$ref": "productTypes", "$id": ObjectId("4d120a2d2b8d8d3010000000"), "$db":…
cnkt
  • 2,943
  • 5
  • 30
  • 43
16
votes
3 answers

Doctrine2 - How can I order by a discriminator column?

How should I go about ordering by a discriminator column in a doctrine repository query? I have a pretty straight forward setup, I have different types of payment details, it can either be Credit Card (CC) or Debit Order (DO). So I've implemented…
Odyss3us
  • 6,457
  • 18
  • 74
  • 112
16
votes
2 answers

Symfony form query_buider and entity repository

I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook. Everything works fine when the query_builder option is a closure where I get my data from DQL. As the…
Florent
  • 806
  • 2
  • 8
  • 14
16
votes
1 answer

Doctrine 2 limit IN subquery

I'm trying to use a subquery in a IN statement in Doctrine2. Here's what the raw SQL query should look like : SELECT * FROM license WHERE id IN (SELECT id FROM license WHERE subscription = x ORDER BY date DESC LIMIT 5) ORDER BY…
Growiel
  • 775
  • 1
  • 7
  • 20
16
votes
3 answers

Doctrine: Is it possible to INDEX BY a related field?

I have a Doctrine model (Assignment), which has a many-to-one relationship with another model (Region). Assignments are owned by users (with each user having only one assignment per region at a time), and I am trying to use indexBy to have the…
LeafStorm
  • 3,057
  • 4
  • 24
  • 28
1
2
3
86 87