Implemented `PreparedStatement::fetchList()`
authorAlexander Ebert <ebert@woltlab.com>
Fri, 23 Aug 2019 15:49:41 +0000 (17:49 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 23 Aug 2019 15:49:41 +0000 (17:49 +0200)
See #3054

wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php

index 4abe0333d6ecdf31a7ee3b5c4b27e466c9ac7b8f..a4b0675b12a56c5a53b9651c50787979f8835ea4 100644 (file)
@@ -23,7 +23,7 @@ class PreparedStatement {
         * database object
         * @var Database
         */
-       protected $database = null;
+       protected $database;
        
        /**
         * SQL query parameters
@@ -35,7 +35,7 @@ class PreparedStatement {
         * pdo statement object
         * @var \PDOStatement
         */
-       protected $pdoStatement = null;
+       protected $pdoStatement;
        
        /**
         * SQL query
@@ -221,6 +221,27 @@ class PreparedStatement {
                return $map;
        }
        
+       /**
+        * Returns a one-dimensional list of all rows holding only the value of the specified column. Please see
+        * `fetchAll()` if you simply want to read all rows into an array. 
+        * 
+        * @param string $column
+        * @return string[]|int[]|float[]
+        */
+       public function fetchList($column) {
+               $list = [];
+               
+               while ($row = $this->fetchArray()) {
+                       if (!array_key_exists($column, $row)) {
+                               throw new \RuntimeException("The requested column '{$column}' is not contained in the result rows.");
+                       }
+                       
+                       $list[] = $row[$column];
+               }
+               
+               return $list;
+       }
+       
        /**
         * Counts number of affected rows by the last sql statement (INSERT, UPDATE or DELETE).
         *