Ticket #416 (closed Bug: fixed)

Opened 9 months ago

Last modified 4 months ago

database should accept more then one direction for oderby

Reported by: Jens Owned by: Shadowhand
Priority: minor Milestone: 2.2
Component: Libraries:Database Version: SVN HEAD
Keywords: database multiple orderby Cc:

Description

Today I can add more then one columns to order by giving an array. If I have: Col1 desc, Col2 asc it would be nice to give the direction as an array too.

Change History

  Changed 9 months ago by Shadowhand

  • keywords multiple added
  • version changed from 2.1 Release to SVN HEAD
  • milestone set to 2.2

  Changed 5 months ago by shaun

I needed this as well, here's the modifications I made to make it work:

/**
 * Method: orderby
 *  Chooses which column(s) to order the <Database.select> <Database.query> by.
 *
 * Parameters:
 *  orderby   - column(s) to order on, can be an array, single column, or comma seperated list of columns
 *  direction - direction(s) of the order
 *
 * Returns:
 *  The <Database> object
 */
public function orderby($orderby, $direction = '') {
	static $directions = array('ASC', 'DESC', 'RAND()', 'NULL');
	if(!is_array($direction)) {
		$direction = strtoupper(trim($direction));

		if ($direction != '') {
			$direction = (in_array($direction, $directions)) ? ' '. $direction : ' ASC';
		}
	}

	if (empty($orderby)) {
		$this->orderby[] = $direction;
		return $this;
	}

	if ( ! is_array($orderby)) {
		$orderby = explode(',', (string) $orderby);
	}

	$order = array();
	foreach ($orderby as $key => $value) {
		if(!is_numeric($key)) {
			$field = $key;
			$value = strtoupper(trim($value));
			$field_dir = (in_array($value, $directions)) ? ' '. $value : ' ASC';
		} elseif(is_array($direction)) {
			$field = $value;
			$field_dir = strtoupper(trim($direction[$key]));
			echo 'key: '.$key;
			$field_dir = (in_array($field_dir, $directions)) ? ' '. $field_dir : ' ASC';
		} else {
			$field = $value;
			$field_dir = $direction;
		}
		
		$field = trim($field);

		if ($field != '') {
			$order[] = $this->driver->escape_column($field).$field_dir;
		}
	}
	$this->orderby[] = implode(',', $order);
	return $this;
}

You can use it as:

$db->orderby(array('col1' => 'desc', 'col2' => 'asc'));
// or
$db->orderby(array('col1', 'col2'), array('desc', 'asc'));

  Changed 5 months ago by Shadowhand

This will be fixed by the rewrite of Database, currently located in the object_db module.

  Changed 5 months ago by Shadowhand

  • status changed from new to closed
  • resolution set to duplicate

Functionality is part of #204.

  Changed 4 months ago by Shadowhand

  • status changed from closed to reopened
  • resolution deleted

  Changed 4 months ago by Shadowhand

  • owner changed from zombor to Shadowhand
  • status changed from reopened to new

  Changed 4 months ago by Shadowhand

  • status changed from new to assigned

  Changed 4 months ago by Shadowhand

  • status changed from assigned to closed
  • resolution set to fixed

Fixed in r3124.

follow-up: ↓ 10   Changed 4 months ago by Nowaker

  • status changed from closed to reopened
  • resolution deleted

It doesn't work - DB error eppears.

$orm->orderby(array('free_places', 'id'), array('desc', 'desc'));

SQL:

ORDER BY 0 ASC, `1` ASC

in reply to: ↑ 9   Changed 4 months ago by Edy

  • status changed from reopened to closed
  • resolution set to fixed

try

$orm->orderby(array('free_places' =>'DESC',  'id' => 'DESC'));

  Changed 4 months ago by Nowaker

  • type changed from Feature Request to Bug

It wasn't explained in phpDoc. If there were, I would not try orderby(array(...), array(...)).

Note: See TracTickets for help on using tickets.