Fetch a given number of posts
authorMichael <heluecht@pirati.ca>
Sun, 21 Feb 2021 07:46:58 +0000 (07:46 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 21 Feb 2021 07:46:58 +0000 (07:46 +0000)
src/Database/DBA.php
src/Database/Database.php
src/Model/Post.php

index 3bdcfb6..56e8891 100644 (file)
@@ -767,13 +767,15 @@ class DBA
        /**
         * Fills an array with data from a query
         *
-        * @param object $stmt statement object
-        * @param bool   $do_close
+        * @param object $stmt     statement object
+        * @param bool   $do_close Close database connection after last row
+        * @param int    $count    maximum number of rows to be fetched
+        *
         * @return array Data array
         */
-       public static function toArray($stmt, $do_close = true)
+       public static function toArray($stmt, $do_close = true, int $count = 0)
        {
-               return DI::dba()->toArray($stmt, $do_close);
+               return DI::dba()->toArray($stmt, $do_close, $count);
        }
 
        /**
index ec95a83..653fd03 100644 (file)
@@ -1613,12 +1613,13 @@ class Database
        /**
         * Fills an array with data from a query
         *
-        * @param object $stmt statement object
-        * @param bool   $do_close
+        * @param object $stmt     statement object
+        * @param bool   $do_close Close database connection after last row
+        * @param int    $count    maximum number of rows to be fetched
         *
         * @return array Data array
         */
-       public function toArray($stmt, $do_close = true)
+       public function toArray($stmt, $do_close = true, int $count = 0)
        {
                if (is_bool($stmt)) {
                        return [];
@@ -1627,6 +1628,9 @@ class Database
                $data = [];
                while ($row = $this->fetch($stmt)) {
                        $data[] = $row;
+                       if (($count != 0) && (count($data) == $count)) {
+                               return $data;
+                       }
                }
 
                if ($do_close) {
index 5845e71..8f13bfc 100644 (file)
@@ -443,17 +443,17 @@ class Post
                $update_fields = DBStructure::getFieldsForTable('post-user', $fields);
                if (!empty($update_fields)) {
                        $affected_count = 0;
-                       $rows = DBA::selectToArray('post-view', ['post-user-id'], $condition);
-                       $puids = array_column($rows, 'post-user-id');
-                       while (!empty($puids)) {
-                               $segment = array_splice($puids, 0, 100);
-                               if (!DBA::update('post-user', $update_fields, ['id' => $segment])) {
+                       $posts = DBA::select('post-view', ['post-user-id'], $condition);
+                       while ($rows = DBA::toArray($posts, false, 100)) {
+                               $puids = array_column($rows, 'post-user-id');
+                               if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
                                        DBA::rollback();
                                        Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
                                        return false;
                                }
                                $affected_count += DBA::affectedRows();
                        }
+                       DBA::close($posts);
                        $affected = $affected_count;                    
                }