/** * Set the sort order of this data list * * @see SS_List::sort() * @see SQLQuery::orderby * @example $list->sort('Name'); // default ASC sorting * @example $list->sort('Name DESC'); // DESC sorting * @example $list->sort('Name', 'ASC'); * @example $list->sort(array('Name'=>'ASC,'Age'=>'DESC')); * * @param String|array Escaped SQL statement. If passed as array, all keys and values are assumed to be escaped. * @return DataList */ public function sort() { if (count(func_get_args()) == 0) { return $this; } if (count(func_get_args()) > 2) { throw new InvalidArgumentException('This method takes zero, one or two arguments'); } if (count(func_get_args()) == 2) { // sort('Name','Desc') if (!in_array(strtolower(func_get_arg(1)), array('desc', 'asc'))) { user_error('Second argument to sort must be either ASC or DESC'); } $this->dataQuery->sort(func_get_arg(0), func_get_arg(1)); } else { if (is_string(func_get_arg(0)) && func_get_arg(0)) { // sort('Name ASC') if (stristr(func_get_arg(0), ' asc') || stristr(func_get_arg(0), ' desc')) { $this->dataQuery->sort(func_get_arg(0)); } else { $this->dataQuery->sort(func_get_arg(0), 'ASC'); } } else { if (is_array(func_get_arg(0))) { // sort(array('Name'=>'desc')); $this->dataQuery->sort(null, null); // wipe the sort foreach (func_get_arg(0) as $col => $dir) { // Convert column expressions to SQL fragment, while still allowing the passing of raw SQL fragments. try { $relCol = $this->getRelationName($col); } catch (InvalidArgumentException $e) { $relCol = $col; } $this->dataQuery->sort($relCol, $dir, false); } } } } return $this; }
/** * Set the sort order of this data list * * @see SS_List::sort() * @see SQLQuery::orderby * * @example $list->sort('Name'); // default ASC sorting * @example $list->sort('Name DESC'); // DESC sorting * @example $list->sort('Name', 'ASC'); * @example $list->sort(array('Name'=>'ASC,'Age'=>'DESC')); * * @return DataList */ public function sort() { if(count(func_get_args()) == 0) { return $this; } if(count(func_get_args()) > 2) { throw new InvalidArgumentException('This method takes zero, one or two arguments'); } if(count(func_get_args()) == 2) { // sort('Name','Desc') if(!in_array(strtolower(func_get_arg(1)),array('desc','asc'))){ user_error('Second argument to sort must be either ASC or DESC'); } $this->dataQuery->sort(func_get_arg(0), func_get_arg(1)); } else if(is_string(func_get_arg(0)) && func_get_arg(0)){ // sort('Name ASC') if(stristr(func_get_arg(0), ' asc') || stristr(func_get_arg(0), ' desc')) { $this->dataQuery->sort(func_get_arg(0)); } else { $this->dataQuery->sort(func_get_arg(0), 'ASC'); } } else if(is_array(func_get_arg(0))) { // sort(array('Name'=>'desc')); $this->dataQuery->sort(null, null); // wipe the sort foreach(func_get_arg(0) as $col => $dir) { $this->dataQuery->sort($this->getRelationName($col), $dir, false); } } return $this; }
/** * Tests that getFinalisedQuery can include all tables */ public function testConditionsIncludeTables() { // Including filter on parent table only doesn't pull in second $query = new DataQuery('DataQueryTest_C'); $query->sort('"SortOrder"'); $query->where(array('"DataQueryTest_C"."Title" = ?' => array('First'))); $result = $query->getFinalisedQuery(array('Title')); $from = $result->getFrom(); $this->assertContains('DataQueryTest_C', array_keys($from)); $this->assertNotContains('DataQueryTest_E', array_keys($from)); // Including filter on sub-table requires it $query = new DataQuery('DataQueryTest_C'); $query->sort('"SortOrder"'); $query->where(array('"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => array('First', 2))); $result = $query->getFinalisedQuery(array('Title')); $from = $result->getFrom(); // Check that including "SortOrder" prompted inclusion of DataQueryTest_E table $this->assertContains('DataQueryTest_C', array_keys($from)); $this->assertContains('DataQueryTest_E', array_keys($from)); $arrayResult = iterator_to_array($result->execute()); $first = array_shift($arrayResult); $this->assertNotNull($first); $this->assertEquals('First', $first['Title']); $second = array_shift($arrayResult); $this->assertNotNull($second); $this->assertEquals('Last', $second['Title']); $this->assertEmpty(array_shift($arrayResult)); }