Skip to content

Loading…

Fixing the problem with order and \Zend\Db\Sql\Expression #3017

Closed
wants to merge 2 commits into from

3 participants

@shoseph

No description provided.

@Maks3w
Zend Framework member

Please add a test case

@ralphschindler
Zend Framework member

So your feature request is that you want to use expressions with order()? What do you need to do with an exression object that you can't already achieve with a string or array notation?

@shoseph

I found the problem when I needed to use the function "rand ()" to return the MySql random fields.

I checked that was implemented logic Sql \ Expression, I enabled only for "order ()".

@shoseph

Ps. In Zend Framework 1 was recommended to use "zend_db_expr()"

@ralphschindler
Zend Framework member

Merged to develop, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 6 additions and 0 deletions.
  1. +2 −0 library/Zend/Db/Sql/Select.php
  2. +4 −0 tests/ZendTest/Db/Sql/SelectTest.php
View
2 library/Zend/Db/Sql/Select.php
@@ -340,6 +340,8 @@ public function order($order)
} else {
$order = (array) $order;
}
+ } elseif ($order instanceof Expression){
+ $order = array($order);
}
foreach ($order as $k => $v) {
if (is_string($k)) {
View
4 tests/ZendTest/Db/Sql/SelectTest.php
@@ -260,6 +260,10 @@ public function testOrder()
$select = new Select;
$select->order(array('name ASC', 'age DESC'));
$this->assertEquals(array('name ASC', 'age DESC'), $select->getRawState('order'));
+
+ $select = new Select;
+ $select->order(new \Zend\db\sql\Expression('id DESC'));
+ $this->assertEquals('id DESC', current($select->getRawState('order'))->getExpression());
}
/**
Something went wrong with that request. Please try again.