Giter Site home page Giter Site logo

datamapper-orderby-field's Introduction

Order By Field in Datamapper ORM with Codeigniter Framework

Datamapper orm using order_by of codeigniter as an alias $this->db->order_by() , and database class is not extendable, so you have to edit the core if you have other implementation.

The Previous problem is:

The order_by function is used to sort any column ASC or DESC and has no implementation to sort by field using MySQL FIELD function.

How to use - Example

$sortIdsAs = array(50,10,60,12);
$articles = new Article();
$articles->where_in("id",$sortIdsAs); //it will return the objects ordered by ASC
$articles->order_by("id",$sortIdsAs,true); // it will return the objects ordered by sortIdsAs
$articles->get();

Changes:

Step One:

Open file DB_active_rec.php located in application/core/database/DB_active_rec.php

Step Two:

Edit function order_by:

/** This function has been edited by: Mohammad AlBanna
	 * Web Address: www.mbanna.info
	 * Sets the ORDER BY value
	 *
	 * @param	string
	 * @param	string	direction: asc or desc
	 * @return	object
	 */
	public function order_by($orderby, $direction = '',$byField = false)
	{	if(is_array($direction) && $byField){
			$direction = "FIELD(".$orderby.",'".implode("' , '",$direction)."')";
		}
		elseif (strtolower($direction) == 'random')
		{
			$orderby = ''; // Random results want or don't need a field name
			$direction = $this->_random_keyword;
		}
		elseif (trim($direction) != '')
		{
			$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
		}

		if (strpos($orderby, ',') !== FALSE)
		{
			$temp = array();
			foreach (explode(',', $orderby) as $part)
			{
				$part = trim($part);
				if ( ! in_array($part, $this->ar_aliased_tables))
				{
					$part = $this->_protect_identifiers(trim($part));
				}

				$temp[] = $part;
			}

			$orderby = implode(', ', $temp);
		}
		else if ($direction != $this->_random_keyword)
		{
			$orderby = $this->_protect_identifiers($orderby);
		}

		if($byField){
			$orderby_statement = $direction;

		}
		else{
			$orderby_statement = $orderby.$direction;
		}

		// var_dump($orderby_statement);
		// exit;
		$this->ar_orderby[] = $orderby_statement;
		if ($this->ar_caching === TRUE)
		{
			$this->ar_cache_orderby[] = $orderby_statement;
			$this->ar_cache_exists[] = 'orderby';
		}

		return $this;
	}

Step Three:

Change function order_by that located inside datamapper.php library to:

/** This function has been edited by: Mohammad AlBanna
	 * Web Address: www.mbanna.info
	 * Order By
	 *
	 * Sets the ORDER BY portion of the query.
	 *
	 * @param	string $orderby Field to order by
	 * @param	string $direction One of 'ASC' or 'DESC'  Defaults to 'ASC'
	 * @return	DataMapper Returns self for method chaining.
	 */
	public function order_by($orderby, $direction = '',$byField = false)
	{
		$this->db->order_by($this->add_table_name($orderby), $direction,$byField);

		// For method chaining
		return $this;
	}

What if you want to add CASE to order_by:

Question in stackoverflow.com

datamapper-orderby-field's People

Contributors

mohammadalbanna avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.