Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:30:04+00:00 2026-06-09T04:30:04+00:00

Using Datamapper 1.8.1-dev for PHP’s CodeIgniter, I’m trying to build this query to prioritize

  • 0

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 countries
ORDER BY CASE name WHEN 'Australia' THEN 1 ELSE 2 END, name

PHP 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:30:05+00:00Added an answer on June 9, 2026 at 4:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to this, and I'm using datamapper and sinatra to build a basic
I am having trouble with a specific query, using Datamapper ORM for Codeigniter. My
I am using Codeigniter and Datamapper for DB Query. My controller is as follows
I'm using this datamapper http://datamapper.wanwizard.eu problem is datamapper have validation methods similar with codeigniter
Hello I am trying to setup a Padrino project using DataMapper and MySQL on
I am using the CodeIgniter DataMapper ORM, but there is something that I don't
I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between
I am trying to call a select statement using datamapper in sinatra. here is
Hi I am using CodeIgniter with DataMapper and really need some help defining relationship
I'm building a small Ruby application using DataMapper and Sinatra, and I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.