public function count()
    {
        $limit = $this->_limit;
        $offset = $this->_offset;
        $this->limit(0);

        $joins = $this->getJoins();
        $copyJoins = clone($joins);
        
        $keys = array_keys($copyJoins->getArrayCopy());
        foreach ($keys as $key) {
            $joins[$key]['fields'] = array();
        }

        $this->_joins = $joins;

        $fields = $this->_fields;
        $this->_fields = array('COUNT(*) AS totalRows');
        $prefix = $this->_prefixTableName;
        $this->_prefixTableName = false;

        $result = $this->all();

        $this->_prefixTableName = $prefix;
        $this->_fields = $fields;
        $this->_joins = $copyJoins; 
        $this->_limit  = $limit;
        $this->_offset = $offset;
        return (isset($result[0]['totalRows'])) ? $result[0]['totalRows'] : 0;
    }