<?php
$searchString = $modx->getOption('searchString', $scriptProperties, 'hello world');
$c = $modx->newQuery('modResource');
$c->select(array('modResource.id', 'modResource.pagetitle'));
$c->select(array(
'score' => "MATCH (modResource.pagetitle, modResource.content) AGAINST ('{$searchString}' IN BOOLEAN MODE)"
));
$c->having("(score > 0)");
$count = $modx->getCount('modResource', $c);
$c->prepare();
$sql = $c->toSQL();
echo __LINE__ . ' : $sql = ' . $sql . '<br />';
return $count;[2014-01-16 22:12:16] (ERROR in xPDO::getCount @ D:\localhost\example.com\www\core\xpdo\xpdo.class.php : 1024) Error 42S22 executing statement:
Array
(
[0] => 42S22
[1] => 1054
[2] => Unknown column 'score' in 'having clause'
)
<?php
/**
* Replacing MODX's getCount(), because it has bug on counting SQL with
* aggregate functions and HAVING condition.<br>
* Retrieves a count of xPDOObjects by the specified xPDOCriteria.
*
* @param string $className Class of xPDOObject to count instances of.
* @param mixed $criteria Any valid xPDOCriteria object or expression.
* @return integer The number of instances found by the criteria.
* @see xPDO::getCount()
*/
public function getQueryCount($className, $criteria= null) {
$count= 0;
if ($query= $this->modx->newQuery($className, $criteria)) {
$expr= '*';
if ($pk= $this->modx->getPK($className)) {
if (!is_array($pk)) {
$pk= array ($pk);
}
$expr= $this->modx->getSelectColumns($className, $query->getAlias(), '', $pk);
}
if (isset($query->query['columns'])) {
foreach ($query->query['columns'] as $k => $column) {
if (is_numeric($k)) {
unset($query->query['columns'][$k]);
}
}
}
$query->select("COUNT(DISTINCT {$expr})");
if ($stmt= $query->prepare()) {
$tstart = microtime(true);
if ($stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$idx = count($query->query['columns']) - 1;
$idx = $idx >= 0 ? $idx : 0;
if ($results= $stmt->fetchAll(PDO::FETCH_COLUMN, $idx)) {
$count= reset($results);
$count= intval($count);
}
} else {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$this->modx->log(modX::LOG_LEVEL_ERROR, "Error " . $stmt->errorCode() . " executing statement: \n" . print_r($stmt->errorInfo(), true), '', __METHOD__, __FILE__, __LINE__);
}
}
}
return $count;
}This question has been answered by goldsky. See the first response.
<?php
/**
* Replacing MODX's getCount(), because it has bug on counting SQL with
* aggregate functions and HAVING condition.<br>
* Retrieves a count of xPDOObjects by the specified xPDOCriteria.
*
* @param string $className Class of xPDOObject to count instances of.
* @param mixed $criteria Any valid xPDOCriteria object or expression.
* @return integer The number of instances found by the criteria.
* @see xPDO::getCount()
*/
public function getQueryCount($className, $criteria= null) {
$count= 0;
if ($query= $this->modx->newQuery($className, $criteria)) {
$expr= '*';
if (!empty($query->query['having']) || !empty($query->query['groupby'])) {
if ($pk= $this->modx->getPK($className)) {
if (!is_array($pk)) {
$pk= array ($pk);
}
$expr= $this->modx->getSelectColumns($className, 'alias', '', $pk);
}
$query->prepare();
$sql = $query->toSQL();
$stmt= $this->modx->query("SELECT COUNT($expr) FROM ($sql) alias");
} else {
if ($pk= $this->modx->getPK($className)) {
if (!is_array($pk)) {
$pk= array ($pk);
}
$expr= $this->modx->getSelectColumns($className, $query->getAlias(), '', $pk);
}
if (isset($query->query['columns'])) $query->query['columns'] = array();
$query->select(array ("COUNT(DISTINCT {$expr})"));
$stmt= $query->prepare();
}
if ($stmt) {
$tstart = microtime(true);
if ($stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
if ($results= $stmt->fetchAll(PDO::FETCH_COLUMN)) {
$count= reset($results);
$count= intval($count);
}
} else {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$this->modx->log(modX::LOG_LEVEL_ERROR, "Error " . $stmt->errorCode() . " executing statement: \n" . print_r($stmt->errorInfo(), true), '', __METHOD__, __FILE__, __LINE__);
}
}
}
return $count;
}
IFNULL(`my_template_variable_contentvalue`.`value`, `my_template_variable`.`default_text`) AS `my_template_variable`
<?php
/**
* Replacing MODX's getCount(), because it has bug on counting SQL with function.<br>
* Retrieves a count of xPDOObjects by the specified xPDOCriteria.
*
* @param string $className Class of xPDOObject to count instances of.
* @param mixed $criteria Any valid xPDOCriteria object or expression.
* @return integer The number of instances found by the criteria.
* @see xPDO::getCount()
*/
public function getQueryCount($className, $criteria= null) {
$count= 0;
if ($query= $this->modx->newQuery($className, $criteria)) {
$expr= '*';
if ($pk= $this->modx->getPK($className)) {
if (!is_array($pk)) {
$pk= array ($pk);
}
$expr= $this->modx->getSelectColumns($className, 'alias', '', $pk);
}
$query->prepare();
$sql = $query->toSQL();
$stmt= $this->modx->query("SELECT COUNT($expr) FROM ($sql) alias");
if ($stmt) {
$tstart = microtime(true);
if ($stmt->execute()) {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
if ($results= $stmt->fetchAll(PDO::FETCH_COLUMN)) {
$count= reset($results);
$count= intval($count);
}
} else {
$this->modx->queryTime += microtime(true) - $tstart;
$this->modx->executedQueries++;
$this->modx->log(modX::LOG_LEVEL_ERROR, "Error " . $stmt->errorCode() . " executing statement: \n" . print_r($stmt->errorInfo(), true), '', __METHOD__, __FILE__, __LINE__);
}
}
}
return $count;
}