Doctrine query builder order by field - php

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.

Related

Question: how to use and with where in laravel

when I want to get data from a database in native PHP I use queries but in laravel, you don't write queries you use their model and their functions to get the data for example:
ex::where('name','test')->get();
in native PHP:
select * from ex where name='test'
and my issue is I don't know how to use and like this query:
select * from ex where name='test' and id='5'
I searched and looked thru the documentation but no answer was found.
Just put another where, its really simple just put it like this
ex::where('name', 'test')->where('id', 5)->get();
//or if you want directly the instance and not a collection
ex::where('name', 'test')->find(5);
If you want to use the OR operator you can also do something like this
ex::where('name', 'test')->orWhere('id', 5)->get();
More in documentations
There is no 'AND' with where in Laravel Eloquent.
You have to write another 'where' with the required condition.
Another way is: run queries using the DB facade. The DB facade provides methods for each type of query: select, update, insert, delete, and statement.
Ex. DB::statement("UPDATE teachers SET price = ".$price." where id=".$id." AND status='A'");
Do not forget to use DB

Laravel Query Builder where max id

How do I accomplish this in Laravel 4.1 Query Builder?
select * from orders where id = (select max(`id`) from orders)
I tried this, working but can't get the eloquent feature.
DB::select(DB::raw('select * from orders where id = (select max(`id`) from orders)'));
Any idea to make it better?
You should be able to perform a select on the orders table, using a raw WHERE to find the max(id) in a subquery, like this:
\DB::table('orders')->where('id', \DB::raw("(select max(`id`) from orders)"))->get();
If you want to use Eloquent (for example, so you can convert your response to an object) you will want to use whereRaw, because some functions such as toJSON or toArray will not work without using Eloquent models.
$order = Order::whereRaw('id = (select max(`id`) from orders)')->get();
That, of course, requires that you have a model that extends Eloquent.
class Order extends Eloquent {}
As mentioned in the comments, you don't need to use whereRaw, you can do the entire query using the query builder without raw SQL.
// Using the Query Builder
\DB::table('orders')->find(\DB::table('orders')->max('id'));
// Using Eloquent
$order = Order::find(\DB::table('orders')->max('id'));
(Note that if the id field is not unique, you will only get one row back - this is because find() will only return the first result from the SQL server.).
Just like the docs say
DB::table('orders')->max('id');
For Laravel ^5
Orders::max('id');
I used it is short and best;
No need to use sub query, just Try this,Its working fine:
DB::table('orders')->orderBy('id', 'desc')->pluck('id');
Laravel 5+:
DB::table('orders')->orderBy('id', 'desc')->value('id');
For objects you can nest the queries:
DB::table('orders')->find(DB::table('orders')->max('id'));
So the inside query looks up the max id in the table and then passes that to the find, which gets you back the object.
You can get the latest record added to the Orders table you can use an eloquent method to retrieve the max aggregate:
$lastOrderId = Order::max('id');
To retrieve a single row by the id column value, use the find method:
$order = Order::find(3);
So combining them, to get the last model added to your table you can use this:
$lastOrder = Order::find(Order::max('id'));

Laravel 4 Build Query where clause on the fly

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.

Paginator with "$fetchJoinCollection = true" won't respect "ORDER BY" in doctrine DQL?

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

Adding custom function to doctrine query builder group by clause

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.

Categories