Make WordPress Core


Ignore:
Timestamp:
02/25/2008 10:00:27 PM (18 years ago)
Author:
westi
Message:

Add keyed object output to wpdb::get_results. Fixes #5286 props andy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r6726 r7028  
    88define('EZSQL_VERSION', 'WP1.25');
    99define('OBJECT', 'OBJECT', true);
     10define('OBJECT_K', 'OBJECT_K', false);
    1011define('ARRAY_A', 'ARRAY_A', false);
    1112define('ARRAY_N', 'ARRAY_N', false);
     
    397398     * Return an entire result set from the database
    398399     * @param string $query (can also be null to pull from the cache)
    399      * @param string $output ARRAY_A | ARRAY_N | OBJECT
     400     * @param string $output ARRAY_A | ARRAY_N | OBJECT_K | OBJECT
    400401     * @return mixed results
    401402     */
     
    408409            return null;
    409410
    410         // Send back array of objects. Each row is an object
    411411        if ( $output == OBJECT ) {
     412            // Return an integer-keyed array of row objects
    412413            return $this->last_result;
     414        } elseif ( $output == OBJECT_K ) {
     415            // Return an array of row objects with keys from column 1
     416            // (Duplicates are discarded)
     417            foreach ( $this->last_result as $row ) {
     418                $key = array_shift( get_object_vars( $row ) );
     419                if ( !isset( $new_array[ $key ] ) )
     420                    $new_array[ $key ] = $row;
     421            }
     422            return $new_array;
    413423        } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
     424            // Return an integer-keyed array of...
    414425            if ( $this->last_result ) {
    415426                $i = 0;
    416427                foreach( $this->last_result as $row ) {
    417                     $new_array[$i] = (array) $row;
    418428                    if ( $output == ARRAY_N ) {
    419                         $new_array[$i] = array_values($new_array[$i]);
     429                        // ...integer-keyed row arrays
     430                        $new_array[$i] = array_values( get_object_vars( $row ) );
     431                    } else {
     432                        // ...column name-keyed row arrays
     433                        $new_array[$i] = get_object_vars( $row );
    420434                    }
    421                     $i++;
     435                    ++$i;
    422436                }
    423437                return $new_array;
    424             } else {
    425                 return null;
    426438            }
    427439        }
Note: See TracChangeset for help on using the changeset viewer.