Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at implementing orderAsc/orderDesc
The current order() method has a limitation which makes it impossible to use expression objects that also have direction. These new methods make it easier to support directed expression objects. I've introduced a new expression object as I felt keeping OrderByExpression simple was worth the extra bit of cost. Additionally this makes SQL injection in the ORM internals more difficult as we rely on the callers to provide a safe expression. I also experimented with an array format, but felt it left us vulnerable to the issues we've had in the past around request data manipulation. Refs #7163
- Loading branch information
Showing
4 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since 3.0.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\Database\Expression; | ||
|
||
use Cake\Database\ExpressionInterface; | ||
use Cake\Database\ValueBinder; | ||
|
||
/** | ||
* An expression object for complex ORDER BY clauses | ||
* | ||
* @internal | ||
*/ | ||
class OrderClauseExpression implements ExpressionInterface | ||
{ | ||
/** | ||
* The field being sorted on. | ||
* | ||
* @var \Cake\Database\ExpressionInterface|string | ||
*/ | ||
protected $_field; | ||
|
||
/** | ||
* The direction of sorting. | ||
* | ||
* @var string | ||
*/ | ||
protected $_direction; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Cake\Database\ExpressionInterface|string $field The field to order on. | ||
* @param string $direction The direction to sort on. | ||
*/ | ||
public function __construct($field, $direction) | ||
{ | ||
$this->_field = $field; | ||
$this->_direction = strtolower($direction) === 'asc' ? 'ASC' : 'DESC'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function sql(ValueBinder $generator) | ||
{ | ||
$field = $this->_field; | ||
if ($field instanceof ExpressionInterface) { | ||
$field = $field->sql($generator); | ||
} | ||
return sprintf("%s %s", $field, $this->_direction); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function traverse(callable $visitor) | ||
{ | ||
if ($this->_field instanceof ExpressionInterface) { | ||
$callable($this->_field); | ||
$this->_field->traverse($callable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters