Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added sorting to queries

git-svn-id: http://mongodb-morph.googlecode.com/svn/trunk@102 406649b4-5d4b-11de-8abc-d1c337b90d21
  • Loading branch information...
commit e121b8cb03e19edd3432510121be1bcc62db202b 1 parent 2d8bc88
XirisR authored
View
3  mongodb-morph/src/Morph/Enum.php
@@ -15,4 +15,7 @@ class Morph_Enum
const STATE_NEW = 'New';
const STATE_CLEAN = 'Clean';
const STATE_DIRTY = 'Dirty';
+
+ const DIRECTION_ASC = 1;
+ const DIRECTION_DESC = -1;
}
View
27 mongodb-morph/src/Morph/Query.php
@@ -162,10 +162,35 @@ public function getRawQuery()
if (count($this->criteria) > 0) {
foreach ($this->criteria as $propertyName => $criteria) {
- $query[$propertyName] = $criteria->getConstraints();
+ $constraints = $criteria->getConstraints();
+ if (!empty($constraints)) {
+ $query[$propertyName] = $constraints;
+ }
}
}
return $query;
}
+
+ /**
+ * Returns sorting details suitable for passing to MongoDB
+ *
+ * @return array
+ */
+ public function getRawSort()
+ {
+ $sort = array();
+ if (count($this->criteria) > 0) {
+ foreach ($this->criteria as $propertyName => $criteria) {
+ $direction = $criteria->getSort();
+ if (null !== $direction) {
+ $sort[$propertyName] = $direction;
+ }
+ }
+ }
+ if (empty($sort)) {
+ $sort = null;
+ }
+ return $sort;
+ }
}
View
36 mongodb-morph/src/Morph/Query/Property.php
@@ -45,6 +45,11 @@ class Morph_Query_Property
private $mode;
/**
+ * @var int
+ */
+ private $sortDirection;
+
+ /**
* Create a new property object
*
* @param $query
@@ -237,6 +242,27 @@ public function size($value)
}
/**
+ * Sets the sort direction if needed
+ *
+ * @param $direction
+ * @return unknown_type
+ */
+ public function sort($direction)
+ {
+ switch ($direction) {
+ case Morph_Enum::DIRECTION_ASC:
+ $this->sortDirection = Morph_Enum::DIRECTION_ASC;
+ break;
+ case Morph_Enum::DIRECTION_DESC:
+ $this->sortDirection = Morph_Enum::DIRECTION_DESC;
+ break;
+ default:
+ throw new InvalidArgumentException("Sort direction should be -1 or 1");
+ }
+ return $this;
+ }
+
+ /**
* Returns an array of all associated constraints
*
* @return array
@@ -246,6 +272,16 @@ public function getConstraints()
return $this->constraints;
}
+ /**
+ * Returns the sort direction of this property
+ *
+ * @return int
+ */
+ public function getSort()
+ {
+ return $this->sortDirection;
+ }
+
//////////////////////////////////////////
// INTERNAL CONSTRAINT HELPER FUNCTIONS //
//////////////////////////////////////////
View
16 mongodb-morph/src/Morph/Storage.php
@@ -187,12 +187,20 @@ public function findByQuery(Morph_Object $object, Morph_Query $query = null)
$query = (is_null($query)) ? new Morph_Query() : $query;
$cursor = $this->Db->selectCollection($object->collection())->find($query->getRawQuery());
- if (!is_null($query->getLimit())) {
- $cursor->limit($query->getLimit());
+
+ $limit = $query->getLimit();
+ if (!is_null($limit)) {
+ $cursor->limit($limit);
+ }
+
+ $skip = $query->getSkip();
+ if (!is_null($skip)) {
+ $cursor->skip($skip);
}
- if (!is_null($query->getSkip())) {
- $cursor->skip($query->getSkip());
+ $sort = $query->getRawSort();
+ if (!is_null($sort)) {
+ $cursor->sort($sort);
}
$iterator = new Morph_Iterator($object, $cursor);

0 comments on commit e121b8c

Please sign in to comment.
Something went wrong with that request. Please try again.