Skip to content

Commit

Permalink
Issue #164: Added destructor to make sure PostIterator PDO statement …
Browse files Browse the repository at this point in the history
…cursor is closed
  • Loading branch information
Mark Wilkie authored and ginatrapani committed Aug 22, 2010
1 parent 18e55f6 commit 5e2ec33
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions tests/TestOfLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testLoaderUnregister() {
public function testLoaderInstantiateClasses() {
Loader::register();

$this->assertClassInstantiates('Captcha');
$this->assertClassInstantiates('Instance');
$this->assertClassInstantiates('Config');

$this->assertIsA(new Crawler, 'Crawler');
Expand All @@ -113,7 +113,7 @@ public function assertClassInstantiates($class) {
if ( !$this->config_file_exists ) {
$this->pass('Missing Configuration File');
} else {
$this->fail('Configuration File Exists But Failed to Load Captcha');
$this->fail('Configuration File Exists But Failed to Load class');
}
}
}
Expand Down
27 changes: 23 additions & 4 deletions webapp/model/class.PostIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class PostIterator implements Iterator {
*/
private $valid = false;

/*
* @var boolean defines if cursor has been closed
*/
private $closed_cursor = false;


/**
* Contructor
*/
Expand Down Expand Up @@ -55,18 +61,20 @@ public function key() {

/*
* Returns true if there is a row to fetch
* @return boolean There is another value/row
* @return bool There is another value/row
*/
public function valid() {
$this->valid = false;
if(! is_null($this->stmt)) {
$row = $this->stmt->fetch(PDO::FETCH_ASSOC);
if($row) {
$post = new Post($row);
#$link = new Link($row);
#$post->link = $link;
$this->row = $post;
$this->valid = true;
} else {
// close our cursor...
$this->closed_cursor = true;
$this->stmt->closeCursor();
}
}
return $this->valid;
Expand All @@ -76,6 +84,17 @@ public function valid() {
* Empty method, for this case does nothing
*/
public function next() {
// we handle the row call in vaide, so...
// we handle the row call invalid, so...
}

/*
* Our destructor
* closes PDO stmt handle/cursor if not closed already
*/
public function __destruct() {
// make sure our cursor is closed...
if(! $this->closed_cursor && isset($this->stmt)) {
$this->stmt->closeCursor();
}
}
}

0 comments on commit 5e2ec33

Please sign in to comment.