a>,数据库类是唯一不可扩展的类。 进入 system / database / DB_active_rec.php 并搜索 order_by()函数。为函数的函数定义添加第三个参数: function order_by($ orderby,$ direction ='',$ case = null) code> 滚动到函数的底部...在分配 $ orderby_statement 之前 $ this-> ar_orderby [] 写入: if = true)$ orderby_statement =CASE。 $ orderby_statement; 保存文件并转到 application / libraries / datamapper.php 并搜索 order_by()函数。 $ b 替换为 public function order_by($ orderby,$ direction ='',$ case = null) { $ this-> db-> order_by($ this-> add_table_name($ orderby),$方向,$ case); //方法链接 return $ this; } 现在,在控制器中,如果传递 true 作为 order_by()的第三个参数,并从第一个字符串param中删除CASE关键字,应该得到正确的语法。 希望这有帮助。 Using Datamapper 1.8.1-dev for PHP's CodeIgniter, I'm trying to build this query to prioritize one country name at top of the list:SELECT * FROM countriesORDER BY CASE name WHEN 'Australia' THEN 1 ELSE 2 END, namePHP on my Country model (which extends Datamapper):$countries = new Country();$countries->order_by("CASE name WHEN 'Australia' THEN 1 ELSE 2 END, name");$countries->get_iterated();Datamapper instead parses "CASE" as a table name, building this syntax error query:SELECT * FROM (`countries`)ORDER BY `CASE` name WHEN 'Australia' THEN 1 ELSE 2 END, `countries`.`name`I'm assuming the CASE case isn't being handled since this flow control statement was just added in MySQL 5? Are my only alternatives to either add an order_by column to the countries table or use a PHP sort function after database retrieval? 解决方案 Datamapper’s order_by() is just an alias for $this->db->order_by().Unfortunately, as per CodeIgniter Documentation, the Database classes are the only ones that are non-extendable. You're going to have to get your hands dirty by modifying the core.Head into system/database/DB_active_rec.php and search for the order_by() function. Add a third parameter to the function's function definition:function order_by($orderby, $direction = '', $case = null)Scroll to the bottom of the function... Just before assigning the $orderby_statement to $this->ar_orderby[] write:if($case===true) $orderby_statement = "CASE " . $orderby_statement;Save the file and head into application/libraries/datamapper.php and search for the order_by() function. Notice, it's just a wrapper!Replace it with:public function order_by($orderby, $direction = '', $case = null){ $this->db->order_by($this->add_table_name($orderby), $direction, $case); //For method chaining return $this;}Now, in your controller, if you pass true as the third parameter to order_by(), and drop the CASE keyword from the first string param, you should get the correct syntax.Hope this helps. 这篇关于CodeIgniter Datamapper ORM是否支持在ORDER BY子句中使用MySQL CASE语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!