Max F преди 5 години
родител
ревизия
221f0bff8f
променени са 1 файла, в които са добавени 36 реда и са изтрити 1 реда
  1. 36 1
      src/Database/Sql/SqlTableModel.php

+ 36 - 1
src/Database/Sql/SqlTableModel.php

@@ -68,7 +68,7 @@ class SqlTableModel
 	}
 
 
-	public static function getAllPagination($where=null, $options=[])
+	public static function getAllWithFoundRows($where=null, $options=[])
 	{
 		$db = static::getDb();
 		static::checkTable();
@@ -79,6 +79,41 @@ class SqlTableModel
 	}
 
 
+	public static function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+
+		if (! is_array($options)) {
+			$options = [];
+		}
+
+		$page_idx = max(1, intval($page_idx));
+		$nb_per_page = max(1, intval($nb_per_page));
+
+		$offset = ($page_idx - 1) * $nb_per_page;
+		$options['limit'] = $offset . ', ' . $nb_per_page;
+		
+		$result = self::getAllWithFoundRows($where, $options);
+		$found_rows = $result['found_rows'];
+		$data = $result['data'];
+
+		$pagination = [
+			'page' => $page_idx,
+			'limit' => $nb_per_page,
+			'offset' => $offset,
+			'page_rows' => count($data),
+			'total_rows' => $found_rows,
+			'nb_pages' => ceil($found_rows / $nb_per_page),
+		];
+
+		return [
+			'pagination' => $pagination,
+			'data' => $data,
+		];
+	}
+
+
 
 	public static function one($where=[], $options=[])
 	{