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
11
votes
2 answers

Search only by time in DateTime field in Symfony2 and Doctrine

I use Symfony 2 and Doctrine. In my database MySQL, I have a field of type DateTime. In my Repository file, by using QueryBuilder, I would like to search in this table only by date (without time) and only by time (without date), in my Where…
JohnDoe66
  • 855
  • 2
  • 11
  • 16
10
votes
3 answers

custom query in entity field type

I build a form with entity type like this: $form = $this->createFormBuilder() ->add('users', 'entity', array( 'class' => 'UserBundle:Users', 'query_builder' => function(EntityRepository $er) { return…
repincln
  • 2,029
  • 5
  • 24
  • 34
10
votes
1 answer

How to get date difference in Doctrine Query Language (dql)?

I have an entity named Auction with two fields of type datetime: date1 and date2. I would like to SELECT all Auction rows where the difference between date1 and date2 is less then 30 minutes. How can I do this using Doctrine Query…
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95
10
votes
6 answers

How can I order by NULL in DQL?

I'm building an app using Symfony2 framework and using Doctrine ORM. I have a table with airlines for which some IATA codes are missing. I'm outputting a list, ordered by this IATA code, but I'm getting the undesirable result that the records with…
indorock
  • 316
  • 1
  • 4
  • 12
10
votes
6 answers

Doctrine query distinct related entity

I'm probably overlooking something very simple and just been staring at it too much, but I can't get this DQL query to work. I get an exception stating: Cannot select entity through identification variables without choosing at least one root entity…
Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
9
votes
1 answer

Selecting from subquery in DQL

I would like to perform a SELECT from the results of a subquery in DQL. The equivalent of doing the following in SQL: SELECT * FROM ( SELECT foo1,foo2 FROM bar ) where foo1='something'; The problem I am running into is that it complains that Error:…
drewag
  • 93,393
  • 28
  • 139
  • 128
9
votes
1 answer

Group by using month, year in doctrine2

How to write a following SQL query in doctrine2 as DQL . SELECT COUNT(id) FROM stats WHERE YEAR(record_date) = 2009 GROUP BY YEAR(record_date), MONTH(record_date) i.e i would like to group by results based on month,year of datetime field…
afsar
  • 111
  • 1
  • 2
  • 4
9
votes
1 answer

Request on key/value in a JSON type field with Doctrine2

I'm trying to figure out how, in a symfony 3.4 app, to retrieve (through a repository method, with a DQL request for example) entities depending on a value for a specific key in a "json" typed column. Saw there's some stuff possible with postgre but…
arnaudbey
  • 880
  • 1
  • 11
  • 19
9
votes
2 answers

Symfony and Doctrine DQL query builder: how to use multiple setParameters inside if condition checks?

Using Symfony and Doctrine with the DQL query builder, I need to optionally add some WHERE conditions with parameters, with some if condition checks. Pseudo code example: $qb = $this->getEntityManager()->createQueryBuilder(); $qb = $qb …
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
9
votes
3 answers

How to select discriminator column in doctrine 2

I need some help when select only discriminator column from doctrine 2 when run the DQL below SELECT p.type FROM AppBundle\Entity\Product p type is discriminator column in entity AppBundle\Entity\Product @ORM\DiscriminatorColumn(name="type",…
Chin Lee
  • 133
  • 1
  • 1
  • 8
8
votes
1 answer

Doctrine2 - Inheritance mapping, querying sub-classes

I have joined table inheritance (lets call it Action) and I need to get all records of Action that satisfy the condition, but the fields are in sub-classes ? Is there way to access sub-class fields without writing Native Query, but use DQL ? (or…
CappY
  • 1,510
  • 1
  • 17
  • 32
8
votes
1 answer

How to bulk remove entities in a ManyToMany relationship in Doctrine 2?

Say I have the following classes: class Store { /** * @ManyToMany(targetEntity="PaymentMethod") */ protected $paymentMethods; } class PaymentMethod { } When we delete (or just disable, without actually removing from the database)…
BenMorel
  • 34,448
  • 50
  • 182
  • 322
8
votes
5 answers

Doctrine2 Multiple Join works with createQuery but not with queryBuilder

If I'm using querying without queryBuilder with this dql $query = $this->_em ->createQuery("SELECT p, g, c FROM LikeYeah\GoBundle\Entity\Product p JOIN p.garments g LEFT JOIN g.colours c ORDER BY…
Marko Jovanović
  • 2,647
  • 3
  • 27
  • 36
8
votes
2 answers

doctrine dql where clause interpretation with associated entity

Given this entity class SystemRecord { /** * @ORM\Id * @ORM\Column(type="integer", name="ID") * @ORM\GeneratedValue * @var int */ private $id; /** * @ORM\ManyToOne(targetEntity="Application\Entity\User") …
goat
  • 31,486
  • 7
  • 73
  • 96
8
votes
2 answers

Select count() in Doctrine DQL with left join manyToMany unidirectional relation where user does NOT have relation specific group

Situaction: I am trying to select count() in DQL for users NOT in specific group. Standard ManyToMany unidirectional relation between User and Group entities from FOSUserBundle (and SonataUserBundle). System: Symfony 2.5, Doctrine 2.4. Entity…
Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40