From 249a6e284a72ce0989f447d6e93b1b5f57ba444d Mon Sep 17 00:00:00 2001 From: Ilya Sheershoff Date: Thu, 15 Aug 2013 19:25:54 +0600 Subject: [PATCH 1/2] add findAllByPk method to EMongoDocument to meet the needs of RESTFullMongoYii --- EMongoDocument.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/EMongoDocument.php b/EMongoDocument.php index 9ebd017..083f18e 100644 --- a/EMongoDocument.php +++ b/EMongoDocument.php @@ -576,6 +576,18 @@ public function findOne($criteria=array(),$fields=array()){ public function findAll($criteria=array(),$fields=array()){ return $this->find ($criteria,$fields); } + + /** + * Finds all records based on $pk + * @param mixed $pk String or array of pk values + */ + public function findAllByPk($pk=array(),$fields=array()){ + if(is_string($pk)){ + return $this->find (array($this->primaryKey() => $pk),$fields); + }else if(is_array($pk)){ + return $this->find (array($this->primaryKey() => array('$in' => array($pk))),$fields); + } + } /** * Find some records From 7c29eab93571a9cc537b98fa9d617cd1d7862d01 Mon Sep 17 00:00:00 2001 From: Ilya Sheershoff Date: Thu, 15 Aug 2013 19:51:54 +0600 Subject: [PATCH 2/2] add findAllByPk method to EMongoDocument to meet the needs of RESTFullMongoYii needs tests added --- EMongoDocument.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/EMongoDocument.php b/EMongoDocument.php index 083f18e..5925b1b 100644 --- a/EMongoDocument.php +++ b/EMongoDocument.php @@ -579,13 +579,16 @@ public function findAll($criteria=array(),$fields=array()){ /** * Finds all records based on $pk - * @param mixed $pk String or array of pk values + * @param mixed $pk String, MongoID or array of strings or MongoID values (one can mix strings and MongoID in the array) */ public function findAllByPk($pk=array(),$fields=array()){ if(is_string($pk)){ - return $this->find (array($this->primaryKey() => $pk),$fields); + return $this->find (array($this->primaryKey() => $this->getPrimaryKey($pk)),$fields); }else if(is_array($pk)){ - return $this->find (array($this->primaryKey() => array('$in' => array($pk))),$fields); + foreach($pk as $k=>$v){ + $pk[$k] = $this->getPrimaryKey($v); + } + return $this->find (array($this->primaryKey() => array('$in' => $pk)),$fields); } }