Permalink
Browse files

Fix for #339, MySQL ORDER BY bug with related models.

  • Loading branch information...
1 parent f9bf84f commit 4567e7ee2da1273dde45685dbe69dc19a191a031 Marc Ghorayeb committed with nateabele Feb 21, 2012
Showing with 37 additions and 0 deletions.
  1. +2 −0 data/source/Database.php
  2. +35 −0 tests/cases/data/source/DatabaseTest.php
View
@@ -834,7 +834,9 @@ public function order($order, $context) {
if ($field = $model::schema($column)) {
$name = $this->name($model::meta('name')) . '.' . $this->name($column);
$result[] = "{$name} {$dir}";
+ continue;
}
+ $result[] = "{$column} {$dir}";
}
$order = join(', ', $result);
return "ORDER BY {$order}";
@@ -483,6 +483,41 @@ public function testOrder() {
$this->assertEqual($expected, $result);
}
+ public function testOrderOnRelated() {
+ $query = new Query(array(
+ 'model' => $this->_model,
+ 'with' => array('MockDatabaseComment')
+ ));
+
+ $result = $this->db->order('MockDatabaseComment.created DESC', $query);
+ $expected = 'ORDER BY MockDatabaseComment.created DESC';
+ $this->assertEqual($expected, $result);
+
+ $result = $this->db->order(array('MockDatabaseComment.created' => 'DESC'), $query);
+ $expected = 'ORDER BY MockDatabaseComment.created DESC';
+ $this->assertEqual($expected, $result);
+
+ $result = $this->db->order(
+ array(
+ 'MockDatabasePost.title' => 'ASC',
+ 'MockDatabaseComment.created' => 'DESC'
+ ),
+ $query
+ );
+ $expected = 'ORDER BY MockDatabasePost.title ASC, MockDatabaseComment.created DESC';
+ $this->assertEqual($expected, $result);
+
+ $result = $this->db->order(
+ array(
+ 'title' => 'ASC',
+ 'MockDatabaseComment.created' => 'DESC'
+ ),
+ $query
+ );
+ $expected = 'ORDER BY {MockDatabasePost}.{title} ASC, MockDatabaseComment.created DESC';
+ $this->assertEqual($expected, $result);
+ }
+
public function testScopedDelete() {
$query = new Query(array(
'type' => 'delete',

0 comments on commit 4567e7e

Please sign in to comment.