Home > Article > Backend Development > How to use order() in ThinkPHP
This article mainly introduces the use of order() in ThinkPHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.
This article introduces the order() of ThinkPHP. Usage of method. The order method can be used to sort the results of database operations. That is equivalent to an order by clause in the select statement
This article introduces the usage of ThinkPHP's order() method. The order method can be used to sort the results of database operations. That is equivalent to an order by clause in the select statement.
The order method is one of the coherent operation methods of the model and is used to sort the results of database operations. That is equivalent to an order by clause in the select statement.
Usage
$Model->where('status=1')->order('id desc')->limit(5)->select();
Note: There is no order for consecutive operation methods. You can change the calling order at will before calling the select method. .
Supports sorting of multiple fields, for example:
$Model->where('status=1')->order('id desc,status')->limit(5)->select();
If no desc or asc sorting rule is specified, the default is asc.
If your field conflicts with the mysql keyword, it is recommended to call it in array mode, for example:
$Model->where('status=1')->order(array('order','id'=>'desc'))->limit(5)->select();
Supplementary:
Thinkphp cannot use ->order() two solutions for sorting!
Using ThinkPHP, I found that I cannot use ->order($order) to sort.
$order = " info.date2 desc ";
Unfortunately, the result of writing order like this becomes order by date2 limit... desc is missing.
Solution 1:
There cannot be any spaces on both sides of $order, $order = "info.date2 desc"; (correct) . $order = " info.date2 desc"; (Error!)
Solution 2:
Open the file: D:\WebSite\ Zbphp.com\www\ThinkPHP\Extend\Model\ViewModel.class.php
Modify line 136 to $array = explode(' ', trim($order)); add trim and save it Yes, as shown in the picture:
It is recommended to use the second method, but I hope that thinkphp official website can correct this small problem to prevent users from modifying the kernel code themselves
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Usage analysis of initialize() and constructor construct() in ThinkPHP
in PHP move_uploaded_file() function
The above is the detailed content of How to use order() in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!