* rewrite count() method

* change the config of story review
This commit is contained in:
chujilu
2014-08-21 20:41:42 +08:00
parent 7b61d60845
commit dffe80c44b
7 changed files with 39 additions and 18 deletions
+27 -4
View File
@@ -249,10 +249,33 @@ class dao
*/
public function count()
{
$this->setMode('raw');
$this->setMethod('select');
$this->sqlobj = sql::select('count(*) as count');
return $this;
/* Get the SELECT, FROM position, thus get the fields, replace it by count(*). */
$sql = $this->processSQL();
$sql = str_replace('SELECT', 'SELECT SQL_CALC_FOUND_ROWS ', $sql);
/* Remove the part after order and limit. */
$subLength = strlen($sql);
$orderPOS = strripos($sql, DAO::ORDERBY);
$limitPOS = strripos($sql , DAO::LIMIT);
if($limitPOS) $subLength = $limitPOS;
if($orderPOS) $subLength = $orderPOS;
$sql = substr($sql, 0, $subLength);
self::$querys[] = $sql;
/* Get the records count. */
try
{
$row = $this->dbh->query($sql)->fetch(PDO::FETCH_OBJ);
}
catch (PDOException $e)
{
$this->app->triggerError($e->getMessage() . "<p>The sql is: $sql</p>", __FILE__, __LINE__, $exit = true);
}
$sql = 'SELECT FOUND_ROWS() as recTotal;';
$row = $this->dbh->query($sql)->fetch();
return $row->recTotal;
}
/**