dql

How to order by a computed value in DQL

ⅰ亾dé卋堺 提交于 2019-11-30 14:49:50
问题 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 attempt the following: // $qb is an instance of query builder $qb->select('e') ->from('Entity', 'e') ->orderBy('e.prop = :value', 'DESC') ->setParameter('value', 'value'); // grab values I get a Doctrine syntax error, 'end of string'. I looked into

Doctrine 2 - How to use discriminator column in where clause

随声附和 提交于 2019-11-30 12:26:43
问题 I was used discriminator column in where clause like this: //f = root entity $qb = $this->createQueryBuilder('f'); $qb->add('where', 'f.format = \'image\' OR f.format = \'text\''); I've got an error: "Message: [Semantical Error] line 0, col 73 near 'format = 'image'': Error: Class Entities\File\AbstractFile has no field or association named format" How can i use discriminator column in where clause? Thanks. 回答1: I think that you should use INSTANCE OF 回答2: It would look in query builder like

How can I use now() in Doctrine 2 DQL?

回眸只為那壹抹淺笑 提交于 2019-11-30 12:23:47
问题 $ php app/console doctrine:query:dql 'SELECT now()' [Doctrine\ORM\Query\QueryException] [Syntax Error] line 0, col 7: Error: Expected known function, got 'now' How can I manage to use the MySQL now() function with Doctrine DQL? 回答1: The equivalent of MySQL's NOW() is Doctrine DQL's CURRENT_TIMESTAMP() . CURRENT_DATE() only returns the date part. 来源: https://stackoverflow.com/questions/8347872/how-can-i-use-now-in-doctrine-2-dql

How to order by a computed value in DQL

余生颓废 提交于 2019-11-30 11:49:34
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 attempt the following: // $qb is an instance of query builder $qb->select('e') ->from('Entity', 'e') ->orderBy('e.prop = :value', 'DESC') ->setParameter('value', 'value'); // grab values I get a Doctrine syntax error, 'end of string'. I looked into creating a custom function, but that seems like overkill. I'm fairly new to Doctrine, is there a better way

Limit amount of records retrieved when using Doctrine DQL in Symfony2

本小妞迷上赌 提交于 2019-11-30 10:52:27
I have the following query: $latestcontent = $em->createQuery(' SELECT c.title, c.content, c.lastedit, a.firstname, a.surname FROM ShoutMainBundle:Content c, ShoutMainBundle:Admin a WHERE c.author = a.id ORDER BY c.lastedit ASC' ); What I need to do, is limit the amount of records returned from this query. However, when I add LIMIT 10 to the SQL query, it returns this error: Error: Expected end of string, got 'LIMIT'. So, I had a look and found that you could do add ->limit(10) to the code (after the query). But this then throws up this PHP error: Fatal error: Call to undefined method Doctrine

Doctrine 2 DQL - Select rows where a many-to-many field is empty?

被刻印的时光 ゝ 提交于 2019-11-30 07:51:07
问题 I have two classes in this example - DeliveryMethod and Country. They have a many-to-many relationship with each other. What I want to do is select all DeliveryMethods that do not have any Countries mapped to them. I can do the opposite, that is select all delivery methods that have at least one country - SELECT m FROM DeliveryMethod m JOIN m.countries But I can't figure out how to do select where the countries field is empty. In plain SQL I would do the following (deliverymethod_country is

Using JOIN in Symfony2/Doctrine SQL

谁说胖子不能爱 提交于 2019-11-30 06:46:27
I have a problem while trying to USE QueryBuilder OR DQL. I have the following relation: User <-1:n-> Profile <-n:m-> RouteGroup <-1:n-> Route I would like to make a DQL that lists all the routes that a specific user has access. I can get this information with the following code: $usr = $this->container->get('security.context')->getToken()->getUser(); foreach ($usr->getProfiles() as $profile){ foreach ($profile->getRoutegroups() as $routegroup){ var_dump($routegroup->getRoutes()->toArray()); } } For obvious reason i cant use this code, otherwise I will overload my server, LOL. I tried the

Doctrine 2 limit IN subquery

痴心易碎 提交于 2019-11-30 05:59:31
问题 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 name ASC; What I want to do is display the 5 last results ordered by name, so I have to first query the last 5 results and then order by name in the main query. The problem is that I can't seem to LIMIT the inner query. Here's my current code : $qb = $this-

Doctrine 2 - How to use discriminator column in where clause

拈花ヽ惹草 提交于 2019-11-30 03:12:55
I was used discriminator column in where clause like this: //f = root entity $qb = $this->createQueryBuilder('f'); $qb->add('where', 'f.format = \'image\' OR f.format = \'text\''); I've got an error: "Message: [Semantical Error] line 0, col 73 near 'format = 'image'': Error: Class Entities\File\AbstractFile has no field or association named format" How can i use discriminator column in where clause? Thanks. Koc I think that you should use INSTANCE OF It would look in query builder like this: $class = 'Entity\File\Image'; $qb = $this->createQueryBuilder('f'); $qb->where($qb->expr()-

How can I use now() in Doctrine 2 DQL?

≯℡__Kan透↙ 提交于 2019-11-30 02:38:16
$ php app/console doctrine:query:dql 'SELECT now()' [Doctrine\ORM\Query\QueryException] [Syntax Error] line 0, col 7: Error: Expected known function, got 'now' How can I manage to use the MySQL now() function with Doctrine DQL? The equivalent of MySQL's NOW() is Doctrine DQL's CURRENT_TIMESTAMP() . CURRENT_DATE() only returns the date part. 来源: https://stackoverflow.com/questions/8347872/how-can-i-use-now-in-doctrine-2-dql