Ver Fonte

dev 2020-04

Max F há 5 anos atrás
pai
commit
b54c307895
2 ficheiros alterados com 53 adições e 29 exclusões
  1. 41 0
      src/Database/Sql/SqlTable.php
  2. 12 29
      src/Database/Sql/SqlTableModel.php

+ 41 - 0
src/Database/Sql/SqlTable.php

@@ -268,6 +268,47 @@ class SqlTable
 	}
 
 
+	public function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
+	{
+		if (! is_array($options)) {
+			$options = [];
+		}
+
+		if (! empty($options['output'])) {
+			$page_idx = 1;
+			$nb_per_page = null;
+			$offset = 0;
+			unset($options['limit']);
+
+		} else {
+			$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 = $this->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' => empty($nb_per_page) ? null : ceil($found_rows / $nb_per_page),
+		];
+
+		return [
+			'pagination' => $pagination,
+			'data' => $data,
+		];
+	}
+
+
 	public function selectCount($where=null, $options=[])
 	{
 		$options['select'] = 'count(*) as nb';

+ 12 - 29
src/Database/Sql/SqlTableModel.php

@@ -73,6 +73,13 @@ class SqlTableModel
 	}
 
 
+
+	public static function count($where=[], $options=[])
+	{
+		// Alias of selectCount
+		return self::selectCount($where, $options);
+	}
+
 	public static function selectCount($where=[], $options=[])
 	{
 		$db = static::getDb();
@@ -84,7 +91,7 @@ class SqlTableModel
 	public static function getAllWithFoundRows($where=null, $options=[])
 	{
 		$db = static::getDb();
-		static::checkTable();		
+		static::checkTable();
 		$tuple = $db->getTable(static::$table_name)->getAllWithFoundRows($where, $options);
 		return $tuple;
 	}
@@ -92,37 +99,13 @@ class SqlTableModel
 
 	public static function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
 	{
-		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,
-		];
+		$db = static::getDb();
+		static::checkTable();
+		$tuple = $db->getTable(static::$table_name)->getAllPagination($where, $nb_per_page, $page_idx, $options);
+		return $tuple;
 	}
 
 
-
 	public static function one($where=[], $options=[])
 	{
 		// Alias of getOne