dql

How do you view a DQL query prepared query at runtime?

喜你入骨 提交于 2019-12-08 02:12:10
问题 function get_event($id){ $query = $this->em->createQuery('SELECT e.name,e.date, e.time, e.venue, e.venueaddress,e.parish,e.genre, e.entryprice, e.phone, e.specialguests, e.weblink, e.otherinfo, e.flyer1, e.flyer2 from Events e WHERE e.id = :id'); $query->setParameter('id', $id); //CAN I VIEW THE QUERY AT THIS TIME? $result = $query->getResult(); return $result; } 回答1: The EchoSqlLogger as suggested by Haim Evgi, well, echo´s the log output, so you should see it on your website. If you just

Getting Doctrine DQL results the SQL way

删除回忆录丶 提交于 2019-12-07 22:10:41
问题 When performing a DQL query such as: SELECT u AS user, t AS transaction FROM Model\User u JOIN Model\Transaction t WITH t.user = u You get alternating rows of results, such as: ['user' => Model\User(1)] ['transaction' => Model\Transaction(1)] ['transaction' => Model\Transaction(2)] ['user' => Model\User(2)] ['transaction' => Model\Transaction(3)] ['transaction' => Model\Transaction(4)] ['transaction' => Model\Transaction(5)] Is it possible to get the result the SQL way, like: ['user' => Model

How to use Doctrine_RawSql for a fulltext search and sorting by relevance

我的梦境 提交于 2019-12-07 20:31:29
I'm trying to get fulltext searches to be sorted by relevance in a Doctrine_RawSql query. This code will perform the search: $q = new Doctrine_RawSql(); $q->select('{p.*}') ->from('cms_page p') ->where('match(p.content) against (?)', $user_query) ->addComponent('p', 'CmsPage p'); This will execute. I would like the results to be sorted by relevance The real sql would have to look something like: select p.id, match(p.content) against (?) as score from cms_page as p order by score desc; So I need to get that match ... against clause in the select... I think. My crapshoot guess at accomplishing

Doctrine - select last 4 rows from table

谁说胖子不能爱 提交于 2019-12-07 16:38:02
问题 I'm using Symfony/Doctrine. I'm trying to select last 4 rows from table, but im getting error. $em = $this->getDoctrine()->getEntityManager(); $query = $em->createQuery( 'SELECT c FROM DprocMainBundle:Courses c ORDER BY id DESC LIMIT 4' ); $course = $query->getResult(); This is my query but it shows error. Expected end of string, got 'LIMIT' How should i use limit, and get the LAST 4 rows? thanks! 回答1: Use setMaxResults() to limit the number of results. $course = $query->setMaxResults(4)-

Join and count in DQL

蹲街弑〆低调 提交于 2019-12-07 04:22:14
问题 I have a MySQL command and I cannot find the equivalent in DQL. I am trying to fetch the list most commented posts. Here is the MySQL command : SELECT posts.id, COUNT(comments.id) AS num FROM posts LEFT JOIN comments ON ( posts.id = comments.post_id ) GROUP BY posts.id Here is the result : id num 1 8 2 9 3 17 4 7 5 6 6 20 7 7 8 10 9 14 10 7 In DQL, it should be : SELECT post, COUNT(comment.id) AS num FROM Entity\Post post LEFT JOIN post.comments comment GROUP BY post.id But this gives : id

How do you view a DQL query prepared query at runtime?

孤街浪徒 提交于 2019-12-06 11:30:26
function get_event($id){ $query = $this->em->createQuery('SELECT e.name,e.date, e.time, e.venue, e.venueaddress,e.parish,e.genre, e.entryprice, e.phone, e.specialguests, e.weblink, e.otherinfo, e.flyer1, e.flyer2 from Events e WHERE e.id = :id'); $query->setParameter('id', $id); //CAN I VIEW THE QUERY AT THIS TIME? $result = $query->getResult(); return $result; } The EchoSqlLogger as suggested by Haim Evgi, well, echo´s the log output, so you should see it on your website. If you just want to see what SQL query Doctrine would generate, use: $query = $this->em->createQuery('SELECT e.name,e.date

Getting Doctrine DQL results the SQL way

雨燕双飞 提交于 2019-12-06 06:10:32
When performing a DQL query such as: SELECT u AS user, t AS transaction FROM Model\User u JOIN Model\Transaction t WITH t.user = u You get alternating rows of results, such as: ['user' => Model\User(1)] ['transaction' => Model\Transaction(1)] ['transaction' => Model\Transaction(2)] ['user' => Model\User(2)] ['transaction' => Model\Transaction(3)] ['transaction' => Model\Transaction(4)] ['transaction' => Model\Transaction(5)] Is it possible to get the result the SQL way, like: ['user' => Model\User(1), 'transaction' => Model\Transaction(1)] ['user' => Model\User(1), 'transaction' => Model

How to return object from a DQL query?

雨燕双飞 提交于 2019-12-06 04:47:20
问题 I have written a DQL query in Doctrine 2: $qb->select('r.position') ->from('\Entities\Races', 'r') ->where($qb->expr()->eq('r.entrantId', ':entrant_id')) ->setParameter('entrant_id', $this->entrantId); $query = $qb->getQuery(); $aRaces = $query->getResult(); Currently it returns the results of the query in an array like so: Array ( [0] => Array ( [position] => 10 ) [1] => Array ( [position] => 4 ) ) I want the result to return an array of Races objects so that I can access the methods

Query Builder / DQL not working with INNER JOIN - Syntax Issue

梦想与她 提交于 2019-12-06 02:51:11
问题 I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined. Actual error is as follows: [Semantical Error] line 0, col 121 near 'I ON C.id = ': Error: Identification Variable MySiteBundle:Items used in join path expression but was not defined before. Here is the PHP code. Note: I have shortened this query to two columns, two tables, and one join to

Group by using month, year in doctrine2

自作多情 提交于 2019-12-05 18:24:18
问题 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 stored in MySQL table. 回答1: In DQL you could also group by month , year , day etc with SUBSTRING . For example - group by month (datetime format Y-m-d H:i:s) : SELECT p, SUBSTRING(p.date, 6, 2) as month FROM Entity p GROUP BY month 来源: https://stackoverflow