I want to group data by year and month of a date column using doctrine.
It currently uses the query builder to produce the statement which is working fine apart from the grouping.
I have installed the Month and Year custom functions from the Doctrine Extensions pack, however, I cannot do the following:
$qb->add('groupBy', 'MONTH(i.instdate)');
I get an Error: Cannot group by undefined identification variable message.
Is this possible with the query builder?
If not can I add DQL to a query builder result? What is the best way to do this?
I don't want to change the whole system to DQL as it is a query built from form options on the fly, so that would be a major change.
There is a workaround that you can use if you can add your custom function to your select clause. You can group by an alias of a custom function result that is in your select clause.
This would look like this
$qb->select('MONTH(i.instdate) as myMonth'....);
$qb->groupBy('myMonth');
It appears that grouping by functions is not possible in the Doctrine version I am using.
It is available in later versions.
I decided to use SQL statments when this was required instead, as changing to a different version of Doctrine, inside ZF this close to a project completion would be too much.
Related
I have a countif query in mysql and I would like to show the table on my html. I'm currently using laravel 6.0 framework.
Here is the picture of the table i want to show:
Here is my code in html:
Here is my code in the controller:
There should be numerous errors with index function in your controller. Specifically with how you are trying to assign $count a value. Read these: Eloquent Methods all() vs get(), Eloquent Selects, Eloquent Ordering, Grouping, Limit and Offset, Eloquent Where.
Laravel has an excellent documentation, if you were to follow it - working with Laravel would become much easier.
Can anyone tell me the similar query in Doctrine to get in the order we pass inside IN() function.
SELECT * FROM user
WHERE id IN (5,2,3,1,4)
ORDER BY FIELD(id,5,2,3,1,4)
It doesn't seems like you can use the FIELD function with Doctrine. But you can still use the Doctrine 2 extension project which add support for additional query functions.
I am porting my code from CodeIgniter to Laravel. and have some question regarding the query builder.
In codeigniter, I can just add where clause to the active record object, as I initialize each property in a class like
$this->db->where('xxxx','bbbb');
in one property initialize function, and
$this->db->where('yyyy','aaaa');
in another property function, and it will all chain up until i fire off the query. But this doesn't seem to be the case of Laravel.
Here is what I do in laravel in each property initialize function
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
and when a actual method is call from outside, it runs
DB:table($this->table)->get();
but this gives me a SELECT * FROM TABLENAME without anywhere clause. So what am I doing wrong here :x or I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
Also in codeigniter, you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel? Thank you!
You can assign your current workings to a variable, and build upon that, let me show you an example based on your example:
Instead of this
DB::table($this->table)->where('xxxx','bbbb');
DB::table($this->table)->where('yyyy','aaa');
Try this...
$query = DB::table($this->table)->where('xxxx','bbbb');
$query->where('yyyy','aaa');
$results = $query->get();
I just shouldn't treat laravel same as codeigniter and think of something totally different to handle this kind of dynamic where clause?
This is not dynamic where clause.
and please, make a habit of reading the documentation.
From the docs of Fluent query builder
$users = DB::table('users')->where('votes', '>', 100)->get();
you can set a section of the query to cache, so even after you fire off the query , those section retains for next query, usually the where clause. Is there a similar function in Laravel?
$users = DB::table('users')->remember(10)->get();
Next time, just open up the docs. they contain all this.
Having a strange issue. We are using MariaDB 5.5 and doctrine/orm 2.3.3, and trying to use the Doctrine Paginator with DQL.
http://docs.doctrine-project.org/en/latest/tutorials/pagination.html
The DQL has an ORDER BY clause [see below for an illustration example]. However, the result is not sorted at all for a given page size. And, if we increase the page size to cover the entire result set, the sorting becomes correct.
$dql = "SELECT a, b FROM EntityA a JOIN a.propertyB b ORDER BY a.createdOn DESC";
$query = $this->em->createQuery($dql)
->setMaxResults($pageSize)
->setFirstResult($offset);
$paginator = new Paginator($query, $fetchJoinCollection=true);
....
I dumped the sql and manually ran it. The sql also gave the correct sorting. So something is causing the sorting issue inside Doctrine's Paginator class.
When I set $fetchJoinCollection=false and passed it to the Paginator constructor, the sorting became correct for any given $pageSize!
Read Doctrine source code [Doctrine/ORM/Tools/Pagination/Paginator.php]. With $fetchJoinCollection=true, doctrine uses a WhereInWalker to get the final result, which doesn't respect the ORDER By clause in the DQL, because the IN() clause doesn't generate the result in the same order as the ids inside the IN() clause.
A sorting solution for the IN() clause can be found in Ordering by the order of values in a SQL IN() clause. But I can't find Doctrine using that.
Anyone with Doctrine internal knowledge would shed some light?! Thanks!
Found out that people have taken care of this issue already.
http://www.doctrine-project.org/jira/browse/DDC-2593
I need to count how many people belong in pre-defined groups (this is easy to do in SQL using the SELECT COUNT statement). My Views query runs fine and displays the actual data in my table, but I simply need to know how many results it found.
However there doesn't seem to be a COUNT option in views. I am guessing I am going to have to use some sort of views hook, and then stick the result in the table.
Here's a quick example of what i'm trying to achieve:
My Table
----------------------
Group A | 20 people
Group B | 63 people
and so on.
(I've tried using the Views_Calc module, but I get errors because it is not quite stable yet.)
Anybody know of an easy way to count results in Views?
Here's a good d.o thread about it:
http://drupal.org/node/131031
Although if you JUST need the count and not the other things Views offers (field formatting & ordering, etc), why not just code up the proper SELECT COUNT statement and call it a day?
(If you DO in fact need those other pieces Views offers, there are many examples on that thread above.)
I'm currently using the Views Group By module for this type of functionality.
I'm actually working on adding other aggregate functions (MIN, MAX, etc.) into it but since you only need the COUNT function, I think it's a pretty good option.
All you have to do (after installing and enabling the module), in the View that you are interested:
Add fields for the criteria that you want to GROUP BY.
Add the SQL Aggregation field as the last field (or swap it into the last).
Choose the fields (you can select multiple fields) Fields to Group On.
SQL Aggregation Function should be set to Count.
Fields to Aggregate with the SQL function set to a field that you are not grouping by. (This will be added into the COUNT function like COUNT(<this field>) in SQL)
The rest is up to you and click Update.
You should have the COUNT output from the field that you selected to aggregate.