Home » Php » php – Database class object

php – Database class object

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve seen people use this piece of code and I’m trying to understand what it does because I don’t see it in any of the codeigniter documenation or in the source code for the database class.

$this->db->ar_orderby
How to&Answers:

This is an array that holds order by columns..

There should be no reason to use this property directly. Instead call $this->db->order_by('column') which appends to the array automatically.

  • defined in system/database/DB_active_rec.php Line 42
  • appended to in method CI_DB_active_record::order_by Line 856
  • used to generate SQL in CI_DB_active_record::_compile_select Line 1781

Answer:

You mean

http://phpxref.com/xref/codeigniter/system/drivers/DB_active_record.php.source.html#l781

It allows you to specify the order by.

Answer:

It still makes sense to use this in the way below –

if (!count($this->db->ar_orderby)) {
    $this->db->order_by($this->order_by );
}

It basically means – If an order is not already set by db library when this method was called, use this. Prevents calling of “order by” twice.

Answer:

You can replace these lines:

if (!count($this->db->ar_orderby)) {
    $this->db->order_by($this->order_by );
}

with:

if(!count($this->db->order_by($this->order_by))) {
    $this->db->order_by($this->order_by);
}