Skip to content

Commit

Permalink
Fixed: #143
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammaye committed Oct 7, 2013
1 parent f39b3e1 commit 5c2499b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
31 changes: 30 additions & 1 deletion EMongoDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function setVersion($n){
$this->{$this->versionField()}=$n;
return true;
}
}
}

/**
* Sets the attribute of the model
Expand Down Expand Up @@ -686,6 +686,35 @@ public function equals($record)
{
return $this->collectionName() === $record->collectionName() && (string)$this->getPrimaryKey() === (string)$record->getPrimaryKey();
}

/**
* Is basically a find one of the last version to be saved
* @return NULL|EMongoDocument
*/
public function getLastVersion(){
$c=$this->find()->sort(array($this->versionField() => -1))->limit(1);
if($c->count()<=0){
return null;
}else{
foreach($c as $row)
return $this->populateRecord($row);
}
}

/**
* Is basically a find one of a version
* @param int $num
* @return NULL|EMongoDocument
*/
public function getVersion($num){
$c=$this->find(array($this->versionField() => $num))->sort(array($this->versionField() => -1))->limit(1);
if($c->count()<=0){
return null;
}else{
foreach($c as $row)
return $this->populateRecord($row);
}
}

/**
* Find one record
Expand Down
20 changes: 20 additions & 0 deletions tests/MongoDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,24 @@ function testSetVersion(){
$oo=versionedDocument::model()->findOne(array('_id'=>$m->_id));
$this->assertEquals(4,$oo->version());
}

function testGetLastVersion(){
$m=new versionedDocument();
$m->name="sammaye";
$this->assertTrue($m->save()); // 1

$m->age=2500;
$this->assertTrue($m->save()); // 2

$doc=versionedDocument::model()->getLastVersion();

$this->assertEquals(2,$doc->version());
}

function testGetVersion(){
$m=new versionedDocument();
$m->name="sammaye";
$this->assertTrue($m->save()); // 1
$this->assertInstanceOf('EMongoDocument',versionedDocument::model()->getVersion(1));
}
}

0 comments on commit 5c2499b

Please sign in to comment.