dql

With Doctrine what are the benefits of using DQL over SQL?

旧巷老猫 提交于 2019-12-04 05:20:28
Can someone provide me a couple clear (fact supported) reasons to use/learn DQL vs. SQL when needing a custom query while working with Doctrine Classes? I find that if I cannot use an ORM's built-in relational functionality to achieve something I usually write a custom method in the extended Doctrine or DoctrineTable class. In this method write the needed it in straight SQL (using PDO with proper prepared statements/injection protection, etc...). DQL seems like additional language to learn/debug/maintain that doesn't appear provide enough compelling reasons to use under most common situations.

Doctrine query distinct related entity

百般思念 提交于 2019-12-04 03:35:32
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 alias. Here's my query. User has a many-to-one relation to Group. Note that this is a unidirectional relation! That may make no sense to you, but it makes sense in our domain logic. SELECT DISTINCT g FROM Entity\User u LEFT JOIN u.group g WHERE u.active = :active Can you tell me what I am missing here? Since this is the first Google match when

doctrine2 - how to use DATE_ADD function

流过昼夜 提交于 2019-12-03 23:57:50
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: Error: Expected'.' or '(', got 'day' I tried different implementations but I allways get some kind of syntax errror. I have checked DoctrineExtensions which contain this function, but I shouldnt need it because the function is already included in doctrine. You have a typo, you have to quotes 'day' ->andWhere("p.created_at <= DATE_ADD(CURRENT_DATE(),4

Doctrine DQL dynamic ORDER BY parameter

若如初见. 提交于 2019-12-03 22:19:40
问题 Im trying to pass the ORDER BY column as a parameter in DQL, like below: $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY :order") ->setParameters( array('order' => 't.name') )->getResult(); I guess it doesn't work because setParameter will escape :order, however the below solution doesn't seem very good: $order = 't.name'; // Dynamic value $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY $order") ->getResult(); Is there a better way to solve this? 回答1: In that

Where-ing in discriminated tables

ⅰ亾dé卋堺 提交于 2019-12-03 21:41:06
How can I select all items from one specific author ? Its possible this way ? Or how can I edit entities if I want many item types and item packages (item has many items) too ? Item /** * @ORM\Table() * @ORM\Entity * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") * @ORM\DiscriminatorMap({ * "cd" = "ItemCD", * "dvd" = "ItemDVD", * "pack" = "ItemPack", * }) */ class Item { /** * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @ORM\Column(name="name", type="string", length=250

Parameters in DQL select statement (Symfony2/Doctrine)

时光怂恿深爱的人放手 提交于 2019-12-03 21:29:00
I'm trying to use external params in a DQL-s SELECT part, but it doesn't work due to an error. What I'm trying: $query = $this->getEntityManager() ->createQuery(" SELECT me.column_one, :param_doesnt_work param FROM CompanyMyBundle:MyEntity me WHERE me.column_one = :param_one AND me.column_two = :param_two ")->setParameters(array( 'param_doesnt_work' => 'A static value', 'param_one' => 'some param', 'param_two' => 'another param', )); I would like to get two columns as a result, the value of 'column_one' and the value of the param in the Select ('A static value' in this case As param). I get

How to paginate a native query in Doctrine 2?

此生再无相见时 提交于 2019-12-03 20:30:17
Doctrine 2 has the Doctrine\ORM\Tools\Pagination\Paginator class which can be used to paginate normal DQL queries. However if I pass it a native query, I get this error: Catchable fatal error: Argument 1 passed to Doctrine\ORM\Tools\Pagination\Paginator::cloneQuery() must be an instance of Doctrine\ORM\Query, instance of Doctrine\ORM\NativeQuery given I've tried removing the type-hinting from the paginator class in the cloneQuery method, but this just gives further errors because other bits of the paginator class expect methods found in Query that aren't in NativeQuery. Is there any easy way

pass array of conditions to doctrine expr()->orx() method

送分小仙女□ 提交于 2019-12-03 18:49:23
问题 I need to construct DQL with a QueryBuilder like this [QUERY]... AND WHERE e.type = x OR e.type = Y OR e.type = N [...] I have types in array How can I pass this array to my query builder? $qb->andWhere($qb->expr()->orx(CONDITIONS)); List of types will be dynamic, calling $qb->andWhere on each foreach types loop will make only more AND WHERE's no more ORs. Can I store multiply orx expressions and then add it to andWhere ? Any idea how to solve this, probably, common problem? 回答1: I hope so,

Doctrine: Multiple (whereIn OR whereIn) query?

别等时光非礼了梦想. 提交于 2019-12-03 11:50:40
问题 I'm having trouble crafting a fairly simple query with Doctrine... I have two arrays ($countries, $cities) and I need to check whether database record values would match any inside either. I'm looking for something like: ->whereIn('country', 'city', $countries, $cities) ... with 'country' being a WHERE IN for $countries and 'city' being a WHERE IN for $city. I could separate the two out but the needed query has lots of other conditions so that's not possible. The resulting SQL I'm after would

Doctrine2 - How can I order by a discriminator column?

醉酒当歌 提交于 2019-12-03 10:44:13
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 a single table inheritance mapping strategy to achieve this, but the problem now comes in when I try to order by the discriminator column, since the discriminator column isn't present in the base class. The repository function: public function getPaymentDetails (ClientContactInterface $clientContact) { $dql = 'SELECT pd from AccountingBundle