Skip to content

Commit

Permalink
add support for returning WireArray/WireData thx @thetuningspoon
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Oct 26, 2018
1 parent a617bd8 commit 46377ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion RockFinder.info.php
@@ -1,7 +1,7 @@
<?php
$info = [
'title' => 'RockFinder',
'version' => '1.0.8',
'version' => '1.0.9',
'summary' => 'Highly Efficient and Flexible SQL Finder Module to return page data without loading PW pages into memory',
'singular' => true,
'autoload' => false,
Expand Down
27 changes: 24 additions & 3 deletions RockFinder.module.php
Expand Up @@ -270,11 +270,25 @@ public function loadSQL($filename) {
/**
* get array of objects for this finder
*/
public function getObjects($array = null) {
public function getObjects($type = null) {
$timer = $this->timer('getObjects');
try {
$results = $this->database->query($this->getSql());
$objects = $results->fetchAll($array ? \PDO::FETCH_ASSOC : \PDO::FETCH_OBJ);

// check for the return type
if($type == 'WireData') {
// return a wirearray of wiredata objects
$objects = $results->fetchAll(\PDO::FETCH_CLASS, '\ProcessWire\WireData');
$objects = (new WireArray())->import($objects);
}
elseif($type == 'array') {
// return array of plain associative php arrays
$objects = $results->fetchAll(\PDO::FETCH_ASSOC);
}
else {
// no type set, return stdClass objects
$objects = $results->fetchAll(\PDO::FETCH_OBJ);
}
}
catch(\PDOException $e) {
// if the sql has an error we return an empty resultset
Expand All @@ -299,7 +313,14 @@ public function getObjects($array = null) {
* return array of arrays
*/
public function getArrays() {
return $this->getObjects(true);
return $this->getObjects('array');
}

/**
* return a WireArray of WireData objects
*/
public function getWireArray() {
return $this->getObjects('WireData');
}

/**
Expand Down

0 comments on commit 46377ba

Please sign in to comment.