Example #1
0
 /**
  * Get the record count of this table. Will use a cached value only for base
  * tables and where there are no filters.
  * @return integer The record count.
  */
 public function getCount()
 {
     // Only cache if this is a base table and there are no filters.
     $canCache = $this->table->isTable() && count($this->table->get_filters()) === 0;
     if ($canCache) {
         if (isset($_SESSION[$this->sessionKey()])) {
             return $_SESSION[$this->sessionKey()];
         }
     }
     // Otherwise, run the COUNT() query.
     $pkCol = $this->table->getPkColumn();
     if ($pkCol instanceof Column) {
         $count_col = '`' . $this->table->getName() . '`.`' . $pkCol->getName() . '`';
     } else {
         $count_col = '*';
     }
     $sql = 'SELECT COUNT(' . $count_col . ') as `count` FROM `' . $this->table->getName() . '`';
     $params = $this->table->applyFilters($sql);
     $count = $this->table->getDatabase()->query($sql, $params)->fetchColumn();
     if ($canCache) {
         $_SESSION[$this->sessionKey()] = $count;
     }
     return (int) $count;
 }