1
1
Fork 0

Fetch a given number of posts

This commit is contained in:
Michael 2021-02-21 07:46:58 +00:00
parent d37f3de3e6
commit 10f7280bbc
3 changed files with 18 additions and 12 deletions

View file

@ -767,13 +767,15 @@ class DBA
/** /**
* Fills an array with data from a query * Fills an array with data from a query
* *
* @param object $stmt statement object * @param object $stmt statement object
* @param bool $do_close * @param bool $do_close Close database connection after last row
* @param int $count maximum number of rows to be fetched
*
* @return array Data array * @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);
} }
/** /**

View file

@ -1613,12 +1613,13 @@ class Database
/** /**
* Fills an array with data from a query * Fills an array with data from a query
* *
* @param object $stmt statement object * @param object $stmt statement object
* @param bool $do_close * @param bool $do_close Close database connection after last row
* @param int $count maximum number of rows to be fetched
* *
* @return array Data array * @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)) { if (is_bool($stmt)) {
return []; return [];
@ -1627,6 +1628,9 @@ class Database
$data = []; $data = [];
while ($row = $this->fetch($stmt)) { while ($row = $this->fetch($stmt)) {
$data[] = $row; $data[] = $row;
if (($count != 0) && (count($data) == $count)) {
return $data;
}
} }
if ($do_close) { if ($do_close) {

View file

@ -443,17 +443,17 @@ class Post
$update_fields = DBStructure::getFieldsForTable('post-user', $fields); $update_fields = DBStructure::getFieldsForTable('post-user', $fields);
if (!empty($update_fields)) { if (!empty($update_fields)) {
$affected_count = 0; $affected_count = 0;
$rows = DBA::selectToArray('post-view', ['post-user-id'], $condition); $posts = DBA::select('post-view', ['post-user-id'], $condition);
$puids = array_column($rows, 'post-user-id'); while ($rows = DBA::toArray($posts, false, 100)) {
while (!empty($puids)) { $puids = array_column($rows, 'post-user-id');
$segment = array_splice($puids, 0, 100); if (!DBA::update('post-user', $update_fields, ['id' => $puids])) {
if (!DBA::update('post-user', $update_fields, ['id' => $segment])) {
DBA::rollback(); DBA::rollback();
Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]); Logger::notice('Updating post-user failed', ['fields' => $update_fields, 'condition' => $condition]);
return false; return false;
} }
$affected_count += DBA::affectedRows(); $affected_count += DBA::affectedRows();
} }
DBA::close($posts);
$affected = $affected_count; $affected = $affected_count;
} }