aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-10-31 18:58:10 +0100
committerWilly Micieli <micieli@laposte.net>2019-10-31 18:58:10 +0100
commita2af2f94899d48058239b99e61f2fa622d2b9f98 (patch)
tree6a89efb1502438de6eb49574d1a42d0a4fcbea08
parenta0f61644e6ef476d0334b8ecf1fa9fd3236af45a (diff)
downloadimperium-a2af2f94899d48058239b99e61f2fa622d2b9f98.zip
imperium-a2af2f94899d48058239b99e61f2fa622d2b9f98.tar.gz
fix model pagination problem
-rw-r--r--imperium/Action/Crud.php6
-rw-r--r--imperium/Model/Model.php4
-rw-r--r--imperium/Query/Query.php179
-rw-r--r--tests/Model/ModelTest.php3
4 files changed, 120 insertions, 72 deletions
diff --git a/imperium/Action/Crud.php b/imperium/Action/Crud.php
index 8159996..ead86dc 100644
--- a/imperium/Action/Crud.php
+++ b/imperium/Action/Crud.php
@@ -181,9 +181,11 @@
append($all,"<th>$column</th>");
append($all,'</thead><tbody>');
- append($all,$this->sql($table)->paginate([$this,'display'],$this->get('page',1),static::$limit));
- append($all,'</tbody></table></div>');
+ $x = $this->sql($table)->paginate([$this,'display'],$this->get('page',1),static::$limit);
+ append($all,$x->content());
+ append($all,'</tbody></table></div>');
+ append($all,$x->pagination());
return $this->view('@crud/show',compact('all','table'));
}
diff --git a/imperium/Model/Model.php b/imperium/Model/Model.php
index 76eeb1c..1168f83 100644
--- a/imperium/Model/Model.php
+++ b/imperium/Model/Model.php
@@ -454,14 +454,14 @@
* @param callable $callable
* @param int $current_page
*
- * @return string
+ * @return Query
*
* @throws DependencyException
* @throws Kedavra
* @throws NotFoundException
*
*/
- public static function paginate($callable, int $current_page) : string
+ public static function paginate($callable, int $current_page) : Query
{
return static::query()->paginate($callable, $current_page, static::$limit);
}
diff --git a/imperium/Query/Query.php b/imperium/Query/Query.php
index 8ac9f21..2994c2b 100644
--- a/imperium/Query/Query.php
+++ b/imperium/Query/Query.php
@@ -11,7 +11,7 @@
use Imperium\Tables\Table;
use Imperium\Zen;
use PDO;
-
+
/**
* Class Query
*
@@ -213,6 +213,19 @@
*/
private $primary;
+ /**
+ *
+ * The pagination
+ *
+ * @var string
+ */
+ private $pagination;
+
+ /**
+ * @var string
+ */
+ private $content;
+
/**
*
* The constructor
@@ -301,7 +314,6 @@
*/
public static function from(string $table, bool $routes = false,bool $admin =false,bool $todo = false,bool $task = false) : Query
{
-
return (new static($routes,$admin,$todo,$task))->select_table($table);
}
@@ -549,20 +561,22 @@
return $this;
}
-
- /**
- *
- * Generate a between clause
- *
- * @method between
- *
- * @param string $column The column name
- * @param mixed $begin The begin value
- * @param mixed $last The last value
- *
- * @return Query
- *
- */
+
+ /**
+ *
+ * Generate a between clause
+ *
+ * @method between
+ *
+ * @param string $column The column name
+ * @param mixed $begin The begin value
+ * @param mixed $last The last value
+ *
+ * @return Query
+ *
+ * @throws Kedavra
+ *
+ */
public function between(string $column, $begin, $last) : Query
{
@@ -824,18 +838,19 @@
return $this;
}
-
- /**
- *
- * Add on the where clause an and clause
- *
- * @param string $column
- * @param string $condition
- * @param string $expected
- *
- * @return Query
- *
- */
+
+ /**
+ *
+ * Add on the where clause an and clause
+ *
+ * @param string $column
+ * @param string $condition
+ * @param string $expected
+ *
+ * @return Query
+ *
+ * @throws Kedavra
+ */
public function and(string $column, string $condition, string $expected) : Query
{
@@ -843,19 +858,20 @@
return $this;
}
-
- /**
- *
- * Add on the where clause a n or clause
- *
- * @param string $value
- * @param string $condition
- * @param string $expected
- *
- *
- * @return Query
- *
- */
+
+ /**
+ *
+ * Add on the where clause a n or clause
+ *
+ * @param string $value
+ * @param string $condition
+ * @param string $expected
+ *
+ *
+ * @return Query
+ *
+ * @throws Kedavra
+ */
public function or(string $value, string $condition, string $expected) : Query
{
@@ -863,17 +879,18 @@
return $this;
}
-
- /**
- *
- *
- * @param string $column
- * @param mixed ...$values
- *
- *
- * @return Query
- *
- */
+
+ /**
+ *
+ *
+ * @param string $column
+ * @param mixed ...$values
+ *
+ *
+ * @return Query
+ *
+ * @throws Kedavra
+ */
public function not(string $column, ...$values) : Query
{
@@ -892,16 +909,18 @@
return $this;
}
-
- /**
- *
- *
- * @param string $column
- * @param mixed ...$values
- *
- * @return Query
- *
- */
+
+ /**
+ *
+ *
+ * @param string $column
+ * @param mixed ...$values
+ *
+ * @return Query
+ *
+ * @throws Kedavra
+ *
+ */
public function only(string $column, ...$values) : Query
{
@@ -946,15 +965,17 @@
*
* @throws Kedavra
*
- * @return string
+ * @return Query
*
*/
- public function paginate($callable, int $page, int $limit) : string
+ public function paginate($callable, int $page, int $limit) : Query
{
- $pagination = (new Pagination($page, $limit, $this->sum()))->paginate();
+ $this->pagination = (new Pagination($page, $limit, $this->sum()))->paginate();
- return collect($this->take($limit, (($page) - 1) * $limit)->by('id')->all())->each($callable)->join('') . ' ' . $pagination;
+ $this->content = collect($this->take($limit, (($page) - 1) * $limit)->by('id')->all())->each($callable)->join('');
+
+ return $this;
}
/**
@@ -969,6 +990,30 @@
return $this;
}
-
+
+
+ /**
+ *
+ * Return the pagination
+ *
+ * @return string
+ *
+ */
+ public function pagination(): string
+ {
+ return $this->pagination;
+ }
+
+ /**
+ *
+ * Return the pagination
+ *
+ * @return string
+ *
+ */
+ public function content(): string
+ {
+ return $this->content;
+ }
}
}
diff --git a/tests/Model/ModelTest.php b/tests/Model/ModelTest.php
index ea79efb..66b5cbc 100644
--- a/tests/Model/ModelTest.php
+++ b/tests/Model/ModelTest.php
@@ -19,7 +19,8 @@ class ModelTest extends Unit
public function test_all()
{
$this->assertNotEmpty(Users::all());
- $this->assertNotEmpty(Users::paginate([$this,'records'],1));
+ $this->assertNotEmpty(Users::paginate([$this,'records'],1)->pagination());
+ $this->assertNotEmpty(Users::paginate([$this,'records'],1)->content());
$this->assertEquals(100,Users::count());
$this->assertNotEmpty(Users::only('id'));
$this->assertInstanceOf(stdClass::class,Users::get(1));