Changeset 3052

Show
Ignore:
Timestamp:
07/11/2008 07:46:30 AM (5 months ago)
Author:
Shadowhand
Message:

Updates to ORM2:

  • Added count_all() and count_last_query()
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/ORM2.php

    r3044 r3052  
    646646 
    647647        /** 
     648         * Count the number of records in the table. 
     649         * 
     650         * @return  integer 
     651         */ 
     652        public function count_all() 
     653        { 
     654                // Return the total number of records in a table 
     655                return $this->db->count_records($this->table_name); 
     656        } 
     657 
     658        /** 
     659         * Count the number of records in the last query, without LIMIT or OFFSET applied. 
     660         * 
     661         * @return  integer 
     662         */ 
     663        public function count_last_query() 
     664        { 
     665                if ($sql = $this->db->last_query()) 
     666                { 
     667                        if (stripos($sql, 'LIMIT') !== FALSE) 
     668                        { 
     669                                // Remove LIMIT from the SQL 
     670                                $sql = preg_replace('/\bLIMIT\s+[^a-z]+/i', '', $sql); 
     671                        } 
     672 
     673                        if (stripos($sql, 'OFFSET') !== FALSE) 
     674                        { 
     675                                // Remove OFFSET from the SQL 
     676                                $sql = preg_replace('/\bOFFSET\s+\d+/i', '', $sql); 
     677                        } 
     678 
     679                        // Get the total rows from the last query executed 
     680                        $result = $this->db->query 
     681                        ( 
     682                                'SELECT COUNT(*) AS '.$this->db->escape_column('total_rows').' '. 
     683                                'FROM ('.trim($sql).') AS '.$this->db->escape_table('counted_results') 
     684                        ); 
     685 
     686                        if ($result->count()) 
     687                        { 
     688                                // Return the total number of rows from the query 
     689                                return (int) $result->current()->total_rows; 
     690                        } 
     691                } 
     692 
     693                return FALSE; 
     694        } 
     695 
     696        /** 
    648697         * Proxy method to Database list_fields. 
    649698         *