public function getRowCount() {
    $mapper = ObjectFactory::getInstance('persistenceFacade')->getMapper($this->type);
    if ($this->table) {
      $table = !is_array($this->table) ? $this-> table : key($this->table);
      // use pk columns for counting
      $countColumns = array_map(function($valueName) use ($mapper, $table) {
        return $mapper->quoteIdentifier($table).'.'.$mapper->quoteIdentifier($mapper->getAttribute($valueName)->getColumn());
      }, $mapper->getPkNames());
    }
    else {
      // fallback if table is not defined: take first column
      $adapter = $this->getAdapter();
      $columns = $this->processSelect($adapter->getPlatform());
      $countColumns = [$columns[$this->quantifier ? 1 : 0][0][0]];
    }

    $countStatement = clone $this;
    $countStatement->reset(Select::COLUMNS);
    $countStatement->reset(Select::LIMIT);
    $countStatement->reset(Select::OFFSET);
    $countStatement->reset(Select::ORDER);
    $countStatement->reset(Select::GROUP);
    // remove all join columns to prevent errors because of missing group by for non aggregated columns (42000 - 1140)
    $joins = $countStatement->joins->getJoins();
    $countStatement->reset(Select::JOINS);
    foreach ($joins as $key => $join) {
      // re add join without cols
      $countStatement->join($join["name"], $join["on"], [], $join["type"]);
    }

    // create aggregation column
    $countStatement->columns(['nRows' => new \Zend\Db\Sql\Expression('COUNT('.$this->quantifier.' '.join(', ', $countColumns).')')]);

    $countStatement->id = $this->id != self::NO_CACHE ? $this->id.'-count' : self::NO_CACHE;
    try {
      $result = $countStatement->query();
      $row = $result->fetch();
      return $row['nRows'];
    }
    catch (\Exception $ex) {
      self::$logger->error("The query: ".$countStatement->__toString()."\ncaused the following exception:\n".$ex->getMessage());
      throw new PersistenceException("Error in persistent operation. See log file for details.");
    }
  }