Symfony2 & Doctrine2 get one result even if more exist inDB - doctrine-orm

Is there any equivalent for the following SQL code in Doctrine2?
ORDER BY id DESC LIMIT 0,1

Found it! The answer is
->setMaxResults(1)
->setFirstResult(0)
->getOneOrNullResult();

Related

Datastore GQL: Distinct + Count query

I have a Google Datastore table on this format:
"KEY","Country","PersonName"
"1","USA","Joe"
"2","USA","Dan"
"3","Canada","Willo"
I want to count how many registered people I have on each country. A combination between count aggregation and distinct.
Expected output:
"USA", 2
"Canada", 1
The solution I'm looking for based on GQL.
Thanks for help.
I believe this will require multiple queries.
The first one is to get the list of Countries[1], and then another one to get count for a country[2].
[1] SELECT DISTINCT ON (Country) Country FROM Test
[2] AGGREGATE COUNT(*) OVER (SELECT * FROM Test WHERE Country="USA")
This I believe is due to the Datastore limitations, as mentioned here.
Please note "Test" is my Google Datastore table ("Kind") name here.

CFWheels findAll Group By Having Clause

In CFWheels I am looking at the findAll() page and it has a group by option by doesn't have a Having Clause option. Is there a way to use having clause using findAll() in CFWheels.
Just to share. Credit to Pankaj in the comment for his answer. Thank you
checklist = model("user_checklist").findAll(select="MAX(user_checklist.r_id)", group="r_id HAVING MAX(user_checklist.r_id) > 13");
gives you
SELECT MAX(user_checklist.r_id) FROM user_checklist GROUP BY r_id HAVING MAX(user_checklist.r_id) > 13
It is very disappointing that the dynamic filtering of the grouped data is not available even in the new release CFWheels 1.4.2.
What I found is this issue, it is a very old issue posted under the cfwheels google group and a google group discussion. Even to this day the workaround for using a having in the group by statement is to use the cfrel by dumphreys which is a ColdFusion Relational Algebra Framework.
I would recommend trying it, it is very easy to use and cleanly written. If you navigate to cfrel.cfc you would find a findAll() function which looks similar to the original findAll() in cfwheels (check out the \wheels\model\read.cfm), but there you'll find it supports having() right out of the box.
Example (cfrel having clause):
/*
SQL: SELECT productId, SUM(total) AS totalSum FROM orders
GROUP BY productId HAVING SUM(total) > ?
ORDER BY totalSum DESC LIMIT 5
PARAMS: [1000]
*/
myOrdersRel = relation(datasource="cfrel")
.select("productId,SUM(total) AS totalSum")
.from("orders")
.group("productId")
.having("SUM(total) > ?", [1000])
.order("totalSum DESC")
.limit(5);
query2 = rel2.query();

Django - MyThing.objects.filter(name=None) vs MyThing.objects.filter(name__isnull=True) - which is better?

I have a table and want to find all rows where the name column is None.
Should I do:
MyThing.objects.filter(name=None)
or
MyThing.objects.filter(name__isnull=True)
What difference does it make?
According to the docs for exact and isnull, these produce the same SQL. Which you use is more of a stylistic choice than a performance or correctness one.
A quick check in the console outputted something like the following for me:
str(MyThing.objects.only('name').filter(name=None).query)
# 'SELECT `myapp_mything`.`id`, `myapp_mything`.`name` FROM `myapp_mything` WHERE `myapp_mything`.`name` IS NULL ORDER BY `myapp_mything`.`name` ASC'
str(MyThing.objects.only('name').filter(name__isnull=True).query)
# 'SELECT `myapp_mything`.`id`, `myapp_mything`.`name` FROM `myapp_mything` WHERE `myapp_mything`.`name` IS NULL ORDER BY `myapp_mything`.`name` ASC'
So they appear to be the same.

MS Access query to ORDER BY date from C++ application

How does a MS Access SQL query for ordering by date looks like? My current query is:
newVal.Format(_T("SELECT * FROM Table WHERE (CDATE(DateStart) BETWEEN #%s# AND #%s#) "), strDateVal, strDateVal2);
where strDateVal and strDateVal2 are CStrings resulting from formating ColeDateTime variables. In this form i get all the dates between strDateVal and strDateVal2 (eg. 10/20/2013 and 10/25/2013), but i can't figure out a way to sort it, ascending or descending.
I've tried using
ORDER BY DateStart ASC
ORDER BY=([DateStart] ASC)
ORDER BY (CDATE(DateStart)) ASC
but none worked, i get an empty result.
I found the answer, and it was quite simple and silly: the correct sintax is ORDER BY Table.Field ASC. So you have to use the table name even if you make a simple SELECT, as if you would make a JOIN.

FLOW3 Doctrine2 :: SELECT b,count(b) FROM (SELECT a,b FROM x\y\z GROUP BY a) GROUP BY b

I'm quite new to FLOW3 and I'm very new to Doctrine.
I'm just running some tests, and I want to learn or understand some techniques that I'll use later on. Now I'm stuck with this doctrine part where I want to generate some stats.
$results = $this->entityManager
->createQuery('SELECT version,count(version)
FROM (SELECT device, version
FROM \MyStuff\Stats\Domain\Model\Stat
WHERE
date > \'2011-10-01 00:00:00\' and
date < \'2011-10-02 00:00:00\'
GROUP BY device) GROUP BY version')
->getResult();
I asked at other places too, where they directed me to the Doctrine Docs.
Well, now there are several examples but these 2 liners are trivial and I couldn't find any example related to that kind of subselect.
So I hope someone here can help me out.
Edit :
I'd like to solve this using dql.
I tried to solve this using a querybuilder but I was told that querybuilder != dql
Edit 2:
Now I was told that doctrine2 doesn't support subselects in "FROM (SUBSELECT)" but that it does "... WHERE IN (SUBSELECT)" and that one could rewrite my query to the IN () form. Well, trying to figure that out now.
Edit 3:
I'm failing in rewriting the from-subquery into an in-subquery. So... dql doesn't do subqueries and there is no other way to do what I want with dql ?! Then dql would lack a very important feature I'd say. Or am I just not seeing sth. here ?
Edit 4:
I finally got the in-subquery but it was about 10 times slower (4 seconds instead of 0.4) and now I was told by some doctrine guys from #doctrine, that I should use the nativeQuery function instead.
Edit 5:
It works using the nativeQuery now, see my answer for that...
Using a native query it works like that...
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addScalarResult('version', 'version');
$rsm->addScalarResult('count', 'count');
$results = $this->entityManager
->createNativeQuery(
'SELECT version, COUNT(version) as count FROM
(
SELECT device, version
FROM mystuff_stats_domain_model_stat
WHERE
date > \'2011-10-01 00:00:00\' and
date < \'2011-10-02 00:00:00\'
GROUP BY device
)
as devices GROUP BY version',$rsm)
->execute();
echo var_dump($results,true);