Skip to content

Commit

Permalink
Added test for FindAllByPk and added ability for findAllByPk to also
Browse files Browse the repository at this point in the history
take MongoId
  • Loading branch information
Sammaye committed Aug 15, 2013
1 parent f0bd09d commit 4b38c95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
24 changes: 12 additions & 12 deletions EMongoDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,19 @@ public function findAll($criteria=array(),$fields=array()){
return $this->find ($criteria,$fields);
}

/**
* Finds all records based on $pk
* @param mixed $pk String, MongoID or array of strings or MongoID values (one can mix strings and MongoID in the array)
*/
/**
* Finds all records based on $pk
* @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() => $this->getPrimaryKey($pk)),$fields);
}else if(is_array($pk)){
foreach($pk as $k=>$v){
$pk[$k] = $this->getPrimaryKey($v);
}
return $this->find (array($this->primaryKey() => array('$in' => $pk)),$fields);
}
if(is_string($pk)||$pk instanceof MongoId){
return $this->find (array($this->primaryKey() => $this->getPrimaryKey($pk)),$fields);
}else if(is_array($pk)){
foreach($pk as $k=>$v){
$pk[$k] = $this->getPrimaryKey($v);
}
return $this->find (array($this->primaryKey() => array('$in' => $pk)),$fields);
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/MongoDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ function testFindBy_id(){
$r=User::model()->findBy_id((string)$c->_id);
$this->assertTrue(!is_null($r));
}

function testFindAllByPk(){
$c=new User;
$c->username='sammaye';
$this->assertTrue($c->save());

$r=User::model()->findAllByPk($c->_id);
$this->assertTrue(!is_null($r));

$r=User::model()->findAllByPk((string)$c->_id);
$this->assertTrue(!is_null($r));

$r=User::model()->findAllByPk(array((string)$c->_id));
$this->assertTrue(!is_null($r));
}

function testUpdateByPk(){
$c=new User;
Expand Down

0 comments on commit 4b38c95

Please sign in to comment.