public function ___find(Selectors $selectors, $options = array())
{
$options = array_merge($this->defaultOptions, $options);
$this->start = 0;
// reset for new find operation
$this->limit = 0;
$this->parent_id = null;
$this->templates_id = null;
$this->checkAccess = true;
$this->getQueryNumChildren = 0;
$this->setupStatusChecks($selectors, $options);
// move getTotal option to a class property, after setupStatusChecks
$this->getTotal = $options['getTotal'];
$this->getTotalType = $options['getTotalType'] == 'count' ? 'count' : 'calc';
unset($options['getTotal']);
// so we get a notice if we try to access it
$database = $this->wire('database');
$matches = array();
$query = $this->getQuery($selectors, $options);
//if($this->wire('config')->debug) $query->set('comment', "Selector: " . (string) $selectors);
if ($options['returnQuery']) {
return $query;
}
if ($options['loadPages'] || $this->getTotalType == 'calc') {
try {
$stmt = $query->prepare();
$this->wire('pages')->executeQuery($stmt);
$error = '';
} catch (Exception $e) {
$this->trackException($e, true);
$error = $e->getMessage();
}
if ($error) {
$this->log($error);
throw new PageFinderException($error);
}
if ($options['loadPages']) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// determine score for this row
$score = 0;
foreach ($row as $k => $v) {
if (strpos($k, '_score') === 0) {
$score += $v;
unset($row[$k]);
}
}
if ($options['returnVerbose']) {
$row['score'] = $score;
// @todo do we need this anymore?
$matches[] = $row;
} else {
$matches[] = $row['id'];
}
}
}
$stmt->closeCursor();
}
if ($this->getTotal) {
if ($this->getTotalType === 'count') {
$query->set('select', array('COUNT(*)'));
$query->set('orderby', array());
$query->set('groupby', array());
$query->set('limit', array());
$stmt = $query->execute();
$errorInfo = $stmt->errorInfo();
if ($stmt->errorCode() > 0) {
throw new PageFinderException($errorInfo[2]);
}
list($this->total) = $stmt->fetch(PDO::FETCH_NUM);
$stmt->closeCursor();
} else {
$this->total = (int) $database->query("SELECT FOUND_ROWS()")->fetchColumn();
}
} else {
$this->total = count($matches);
}
$this->lastOptions = $options;
return $matches;
}