Counting multiple vote choices in Doctrine with createQueryBuilder - php

I'm working on a voting system, where user will be able to vote either yes, no or not sure to a given question.
These are being stored in a voteOptions table as choice with a value of either 0, 1 and 2 which is attached to a vote table that stores additional information. I struggling to work out how to count up the votes in a single function.
The function below counts a single choice, which I currently call once for each voteOption. I'm counting distinct users as they can appear more than once, but only their last vote counts:
public function countPollVotesByIsCurrentAndChoice(Poll $poll, $choice) {
return $this->createQueryBuilder('v')
->leftJoin('v.voteOptions', 'vo')
->andWhere('v.poll = :poll')
->andWhere('v.isCurrentVote = :isCurrentVote')
->andWhere('vo.choice = :choice')
->setParameters(['isCurrentVote' => true, 'poll' => $poll, 'choice' => $choice])
->select('COUNT(DISTINCT v.user) AS vote_count')
->getQuery()
->getSingleScalarResult();
}
But I'd prefer to make a single call to get all 3 counts. I would really appreciate any advice about how best to do this.

You have to use COUNT() together with GROUP BY which will aggregate the choices to three possible answers and the COUNT function counts the number of records for each answer.
$query = $em->createQuery('SELECT count(vo.choice) AS cnt FROM AppBundle:Vote v JOIN v.voteOptions vo WHERE ... GROUP BY vo.choice');
$result = $query->getResult();
This query will not return an array with Vote objects but just an array with rows that contains columnames and values. print_r() will help you

Related

How to group by aggregation of column in foreign table with many-to-many relation in Eloquent?

I am starting to get headaches over this so I thought I just post it here.
I have two tables that are related through a pivot table (as it is a many-to-many relationship). I use Laravel and Eloquent (but general help on how to achieve this with normal SQL queries is also highly appreciated).
I want to order the first table based a column of the second one but the column needs to be "aggregated" for this.
Example with Cars that are shared by many drivers and can have different colors:
Car-Table: [id, color]
Driver-Table: [id, name]
Car.Driver-Table: [car_id, driver_id]
I need a query that gets all drivers that only drive red cars and then all that don't drive red cars.
I have to use a query because I'll maybe do other things (like filtering) on this query afterwards and want to paginate in the end.
I already use queries that get either one of the two groups. They look like this:
In the Driver model:
public function redCars() {
return $this->cars()->where('color', 'red');
}
public function otherColoredCars() {
return $this->cars()->where('color', '<>', 'red');
}
And then in somewhere in a controller:
$driversWithOnlyRedCars = Driver::whereDoesntHave('otherColoredCars')->get();
$driversWithoutRedCars = Driver::whereDoesntHave('redCars')->get();
Is there a way to combine these two?
Maybe I am just thinking completely wrong here.
Update for clarification:
Basically I would need something like this (ot any other way that would lead to the same outcome)
$driversWithOnlyRedCars->addTemporaryColumn('order_column', 0); // Create temporary column with value 0
$driversWithoutRedCars->addTemporaryColumn('order_column', 1);
$combinedQuery = $driversWithOnlyRedCars->combineWith($driversWithoutRedCars); // Somehow combine both queries
$orderedQuery = $combinedQuery->orderBy('order_colum');
$results = $combinedQuery->get();
Update 2
I think, I found out how to get near my goal with raw queries.
Would be something like this:
$a = DB::table(DB::raw("(
SELECT id, 0 as ordering
FROM drivers
WHERE EXISTS (
SELECT * FROM cars
LEFT JOIN driver_car ON car.id = driver_car.car_id
WHERE driver.id = driver_car.driver_id
AND cars.color = 'red'
)
) as only_red_cars"));
$b = DB::table(DB::raw("(
SELECT id, 1 as ordering
FROM drivers
WHERE EXISTS (
SELECT * FROM cars
LEFT JOIN driver_car ON car.id = driver_car.car_id
WHERE driver.id = driver_car.driver_id
AND cars.color <> 'red'
)
) as no_red_cars"));
$orderedQuery = $a->union($b)->orderBy('ordering');
Now the problem is that I need the models ordered like this and paginated in the end so this is not really an answer to my question. I tried to convert this back to models but I didn't succeed yet. What I tried:
$queriedIds = array_column($orderedQuery->get()->toArray(), 'id');
$orderedModels = Driver::orderByRaw('(FIND_IN_SET(drivers.id, "' . implode(',', $queriedIds) . '"))');
But looks like FIND_IN_SET only allows for a column of the table as second parameter. Is there another way to get the Models in the right order out of the ordered union query?
You can use a UNION query:
$driversWithOnlyRedCars = Driver::select('*', DB::raw('0 as ordering'))
->whereDoesntHave('otherColoredCars');
$driversWithoutRedCars = Driver::select('*', DB::raw('1 as ordering'))
->whereDoesntHave('redCars');
$drivers = $driversWithOnlyRedCars->union($driversWithoutRedCars)
->orderBy('ordering')
->orderBy('') // TODO
->paginate();
How do you want drivers with the same ordering to be ordered? You should add a second ORDER BY clause to get a consistent order every time you execute the query.
This is the best I got:
$driversWithOnlyRedCars = Driver::whereHas('cars',function($q){
$q->where('color', 'red');
})->withCount('cars')->get()->where('cars_count',1);

select the row with most recent date in table formed by using join and group by

i am joining three tables and applying group by to pick a single distinct row of each type. when i join with table marketing_follow i have multiple entries and to pick one i am using group by . now i want to pick the entry with most recent date in follow up table . i've been tryn to get this done for quite some time now and really need some help
public function getRegionWiseUserDetails($array){
$this->db->select('mg.region_name, mu.contact_person, mu.contact_number, mu.status, mu.company_name, mu.user_type, mf.follow_up_date, mf.date, mu.user_id');
$this->db->from('marketing_users as mu');
$this->db->join('marketing_group as mg','mg.id = mu.region_id','left');
$this->db->join('marketing_follow_up as mf','mf.user_id_fk =
mu.user_id','left');
foreach($array as $condition){
$this->db->or_where('mg.id',$condition);
}
$this->db->order_by('mf.follow_up_date','asc');
$this->db->group_by('mf.user_id_fk');
$query = $this->db->get();
return $query->result_array();
}
You need to sort the date field in desc order. Also for asc we dont have to pass it as second paramter it defaults accept asc
$this->db->order_by('mf.follow_up_date','desc');

Symfony 2 Multiple Selects with counts on the same table?

Ok I have a flag field on one table, open or closed which is boolean. I am trying to build one query that would take that field and count them based on that flag. Then I will need to group them by account ID
Here is what I am working with now,
$GetTest1 = $GetRepo->createQueryBuilder('s') <- I had 'w' in here but all that did was add an index and not a second alias?
->select(' (count(s.open_close)) AS ClosedCount, (count(w.open_close)) AS OpenCount ')
->where('s.open_close = ?1')
//->andWhere('w.open_close = ?2')
->groupBy('s.AccountID')
->setParameter('1', true)
//->setParameter('2', false)
->getQuery();
Is what I want do-able? I know (or at lest think) that I can build a query with multiple table alias? - Please correct me if I am wrong.
All help most welcome.
Thanks
This DQL query will group the rows in table by accountId and for each of them it will give you count for yes (and you can get count for no by substracting that from total).
BTW I found writing straight DQL queries much more straightforward than writing QueryBuilder queries (which i use only when i need to dynamically construct the query)
$results = $this->get("doctrine")->getManager()
->createQuery("
SELECT t.accountId, SUM(t.openClose) as count_yes, COUNT(t.accountId) as total
FROM AppBundle:Table t
GROUP BY t.accountId
")
->getResult();
foreach ($results as $result) {
//echo print_r($result);
//you can get count_no as $result["total"] - $result["count_yes"];
}

Laravel - Merge two Queries?

I am building a Twitter-like application which has a feed in it. In this feed I need to display shares with this properties:
-Shares from user, who I am following
-Shares from user, which are sorted by the positive rating, but only the top10%
These two queries I need somehow to merge, so it will become an array in the end, which has all the shares which are applying to this criteria, without any duplicates and ordered by ID, desc
My tables are looking like this:
User, Shares, Follows
Shares:
-user_id
-positive
Follows:
-follower_id
-user_id
-level
What I already tried:
$follower_shares = Share::join('follows', 'shares.user_id', '=', 'follows.user_id')
->where('follows.follower_id', Auth::user()->id)
->where('follows.level', 1)
->get(array('shares.*'));
//count = 10% of total shares
$count = Share::count()/10;
$count = round($count);
$top10_shares = Share::orderBy('positive', 'DESC')
->take($count)
->get();
//sorts by id - descending
$top10_shares = $top10_shares->sortBy(function($top)
{
return -($top->id);
});
//merges shares
$shares = $top10_shares->merge($follower_shares);
The problem is now, that I was told that there is a better way to solve this.
Also, $shares is giving me the result which applies to the criteria, but the shares have duplicates (rows, which are applying to both criteria) and arent ordered by id desc in total.
I would be very happy, if you could tell me, how to do this the right way.
Thanks very much!
I found this to be a pretty clean solution:
// Instead of getting the count, we get the lowest rating we want to gather
$lowestRating = Share::orderBy('positive', 'DESC')
->skip(floor(Share::count() * 0.1))
->take(1)->lists('positive');
// Also get all followed ids
$desiredFollow = Auth::user()->follows()->lists('user_id');
// Select where followed or rating higher than minimal
// This lets the database take care of making them distinct
$shares = Share::whereIn('user_id', $desiredFollow)
->orWhere('positive', '>=', $lowestRating[0])
->get();

Pick a random result that hasnt been displayed

Anyone who could help me it will be greatly appreciated.
Goal: I want to display the id from one table randomly as well as to make sure it has not been seen by the current user.
Two tables: offers, has_seen
I want to pick a random id from offers, check it against the has_seen table.
If the ID exists in the has_seen, it need to re pick another random id. The same ID should never be seen by any one user of the current session.
I cannot seem to figure out how to pick a random one, check the other table, and loop back if found.
I have tried this
$query = $this->db->query("SELECT * FROM ".$this->offer_table." WHERE NOT EXISTS (SELECT * FROM ".$this->shown_table." WHERE ".$this->shown_table.".camp_id = ".$this->offer_table.".camp_id AND ".$this->shown_table.".usercode = ".$this->session->userdata("table")." LIMIT 1 ");
I think that this can be achieved in plain SQL by doing a left join and then checking for null.
Something along the lines of
SELECT * FROM table1 LEFT JOIN table2 USING (shared_key) WHERE table2.id IS NULL ORDER BY rand() LIMIT 1
Here's how you could do it using CI's db class:
// the maximum ID that is acceptable
$max = $this->db->get('first_table')->count();
while(true) {
// get a random number
$randomID = rand(0,$max);
// the condition for which we will check the has_seen table
$condition = array(
'id' => $randomID
);
// if count is 0, it has not been seen. We add it to the table and return
// if it has been seen, the loop will repeat
if ($this->db->get_where('has_seen', $condition)->count() === 0) {
$this->db->insert('has_seen', array(
'id' => $randomID
));
return $randomID;
}
}
SELECT * FROM `offers` WHERE `camp_id` NOT IN (SELECT `camp_id` FROM `has_seen` WHERE `user code` = 1) ORDER BY RAND() LIMIT 1
I always prefer reading the contents of a table into an array and working with them from there. Depending on how you plan to use the results, you could cut down on db accesses by reading it all only once and then serving from the array (and then, I presume, updating the has_seen table for next session).
I must apologize for the pseudocode as it's been years since I've written any PHP.
Once you've got your array, the algorithm looks like this:
var array
var end = array.length
function getNextRandomUnseen
{
var i = rand(end)
var temp = array[i]
array[i] = array[end--]
return temp
}
If you want, you can even stick the seen values at the end of the array so they aren't lost.
array[end+1] = temp
Thanks for the answers. I redid the way it is going to be brought to the user and believe the new way is much more efficient when it comes to hundreds of people on my site at once.
Thanks!

Categories