Doctrine 2 Querybuilder How to stop Doctrine 2 turning integer into string - doctrine-orm

I am trying to learn Symfony and I am reading out some data with Doctrine 2. I noticed that the type returned from my Querybuilder is string(2) "10" and not integer, but I need Int since I am trying to build a Google Line Chart. I just can't figure out how to turn it into an Integer. Can anyone help on below Query? Thank you very much in advance.
$qb = $this->createQueryBuilder('o');
return $qb
->select('SUM(CASE WHEN o.plantType LIKE :shrub then 1 ELSE 0 END) as Shrub')
->addSelect('SUM(CASE WHEN o.plantType LIKE :tree then 1 ELSE 0 END) as Tree')
->addSelect('SUM(CASE WHEN o.plantType LIKE :annual then 1 ELSE 0 END) as Annual')

Related

How to write a complex calculated field in Data Studio with regex?

I have been trying to make this regular expression REGEX filter work in Google Data Studio. It is supposed to do the following
Check the field "src_id" and COUNT all the values containing "widget".
Check the field "Page" and COUNT all the values starting with a "/" and ending with "/start".
Check the field "real_title" and NOT COUNT any value containing "-".
I have tried using the code below but it's not providing the correct result:
COUNT(CASE WHEN REGEXP_MATCH(src_id, "^widget" ) THEN 1
WHEN REGEXP_MATCH(Page, ".*(/start)$") then 1
WHEN REGEXP_MATCH(real_title, "^[^-]") then 1
ELSE 0 END)
I expect the result to "52" but it's giving me "582. I need help to spot what I'm doing wrong.
The problem is your count() - it is counting all the entries including zeroes.
either use sum() or just use the case statement and sum where you want it .
Examples
TestField1
COUNT(CASE WHEN REGEXP_MATCH(Page Title , "^How.*" ) THEN 1
ELSE 0 END)
This returns 58 - the number of page titles on my site.
TestField2
Sum(CASE WHEN REGEXP_MATCH(Page Title, "^How.*" ) THEN 1
ELSE 0 END)
This returns 7 - the number of titles on the site that start with "How"
You really don't need the sum() function in most cases because you can sum the field in the places you need it.

Looping through every value

I was trying to run a loop through a variable and was unsure how to code up my thoughts. So, I have variable called newid that goes as
newid
1
1
2
2
3
3
and so on.
foreach x in newid2 {
replace switchers = 1 if doc[_n] != doc[_n+1]
}
I want to modify this code so that this code will run for each two values (in this case run for 1 and 1, 2 and 2). What would be the best way to modify this? Please help me
Something like this can be done with levelsof:
clear
input id str1 doc
1 "A"
1 "B"
2 "A"
3 "C"
3 "A"
end
gen switcher1 = 0
levelsof id
foreach i in `r(levels)' {
quietly tab doc if id==`i'
replace switcher1 = 1 if r(r)>1 & id==`i'
}
However, you there are certainly more efficient ways to accomplish your goal. Here's one example that tags ids that switch doctors:
ssc install egenmore
bysort id: egen num_docs = nvals(doc)
generate switcher2 = cond(num_docs>1,1,0)
The underlying idea is the same. You count the number of distinct values of doc for each id. If that number exceeds one, the id is tagged as a switcher. The second version is arguably more efficient since it does not involve looping over each value of id.

Rails query works then creates an error

I have a query to retrieve all the spots (times) available on a given date.
#takenspots = Spot.joins(:reservations).where('reservations.date = ?', params[:date])
if #takenspots.any?
#availablespots = Spot.where('id != ?', #takenspots.map)
I use #availablespots to populate a select_list.
I have 4 different spots per day and the query works only 2 times. (2/4 spots)
If a try to book a day where i have 2 spots taken out of 4, it says:
SQLite3::SQLException: near ",": syntax error: SELECT "spots".* FROM "spots" WHERE (id != 1,2)
But I can't understand why it works for the first 2 spots and not the 2 remaining...
Someone got an idea ?
Thank you,
I have no idea why this works actually, but it does, so this might help someone:
#availablespots = Spot.where('id not in (?)',#takenspots.map(&:id))

Doctrine2: Doctrine query returns different results from raw query?

I have come across this multiple times in the past, and I just ended up writing a raw SQL query to overcome it, but I really want to find out why this is happening. Look at the statement below
$q = $this->entityManager->createQuery('SELECT um,s,st
FROM Dashboard\Entity\Metric um
JOIN um.stat st
JOIN um.site s
JOIN s.clients c
WHERE c.id = ?1
AND s.competitor = 0
AND s.ignored = 0
AND st.id IN (?2)
GROUP BY s.id, st.id
ORDER BY st.response_field, s.id')
->setParameter(1, $params['c_id'])
->setParameter(2, $statId);
$sql = $q->getSql();
$rs = $q->getResult();
If I take the contents of $sql and paste them into a mySQL tool and run the raw query, it returns 18 results which is correct.
However, $rs only contains 3 results. $statId is a comma-separated string of 6 numbers: (1,2,3,4,5,6). So I am grouping by st.id, and s.id. There will be 3 s.id elements for every st.id element, working out to the 18 results I expected. What's happening is Doctrine is only returning the first st.id which is the group of 3 s.id 's
Any idea what could be causing this?
I got my answer from IRC, but posting here in case it helps someone else. Smart WHERE IN statements aren't planned for support until 2.1 version.
http://groups.google.com/group/doctrine-dev/browse_thread/thread/fbf70837293676fb
But I can accomplish the same goal with query builder.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/query-builder.html

Filtering on the count with the Django ORM

I have a query that's basically "count all the items of type X, and return the items that exist more than once, along with their counts". Right now I have this:
Item.objects.annotate(type_count=models.Count("type")).filter(type_count__gt=1).order_by("-type_count")
but it returns nothing (the count is 1 for all items). What am I doing wrong?
Ideally, it should get the following:
Type
----
1
1
2
3
3
3
and return:
Type, Count
-----------
1 2
3 3
In order to count the number of occurrences of each type, you have to group by the type field. In Django this is done by using values to get just that field. So, this should work:
Item.objects.values('group').annotate(
type_count=models.Count("type")
).filter(type_count__gt=1).order_by("-type_count")
It's logical error ;)
type_count__gt=1 means type_count > 1 so if the count == 1 it won't be displayed :)
use type_count__gte=1 instead - it means type_count >= 1 :)