Skip to content
This repository
Browse code

Better custom find for pagination

Instead of shuffling the paginator settings you can now simply add a new "findType" key and it will automatically change the find() type accordingly
  • Loading branch information...
commit bce82a2322d8d80c54d7ce89624f71a0d4c64998 1 parent 3c6b509
Christian Winther authored July 19, 2012
6  lib/Cake/Controller/Component/PaginatorComponent.php
@@ -152,6 +152,12 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
152 152
 		$extra = array_diff_key($options, compact(
153 153
 			'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
154 154
 		));
  155
+
  156
+		if (!empty($extra['findType'])) {
  157
+			$type = $extra['findType'];
  158
+			unset($extra['findType']);
  159
+		}
  160
+
155 161
 		if ($type !== 'all') {
156 162
 			$extra['type'] = $type;
157 163
 		}
54  lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
@@ -711,6 +711,28 @@ public function testMergeOptionsNamedParams() {
711 711
 	}
712 712
 
713 713
 /**
  714
+ * test mergeOptions with customFind key
  715
+ *
  716
+ * @return void
  717
+ */
  718
+	public function testMergeOptionsCustomFindKey() {
  719
+		$this->request->params['named'] = array(
  720
+			'page' => 10,
  721
+			'limit' => 10
  722
+		);
  723
+		$this->Paginator->settings = array(
  724
+			'page' => 1,
  725
+			'limit' => 20,
  726
+			'maxLimit' => 100,
  727
+			'paramType' => 'named',
  728
+			'findType' => 'myCustomFind'
  729
+		);
  730
+		$result = $this->Paginator->mergeOptions('Post');
  731
+		$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
  732
+		$this->assertEquals($expected, $result);
  733
+	}
  734
+
  735
+/**
714 736
  * test merging options from the querystring.
715 737
  *
716 738
  * @return void
@@ -1078,6 +1100,38 @@ public function testPaginateCustomFindFieldsArray() {
1078 1100
 		$this->assertTrue($result['nextPage']);
1079 1101
 		$this->assertFalse($result['prevPage']);
1080 1102
 	}
  1103
+/**
  1104
+ * test paginate() and custom find with customFind key, to make sure the correct count is returned.
  1105
+ *
  1106
+ * @return void
  1107
+ */
  1108
+	public function testPaginateCustomFindWithCustomFindKey() {
  1109
+		$Controller =& new Controller($this->request);
  1110
+		$Controller->uses = array('PaginatorCustomPost');
  1111
+		$Controller->constructClasses();
  1112
+		$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
  1113
+		$Controller->PaginatorCustomPost->create($data);
  1114
+		$result = $Controller->PaginatorCustomPost->save();
  1115
+		$this->assertTrue(!empty($result));
  1116
+
  1117
+		$Controller->paginate = array(
  1118
+			'conditions' => array('PaginatorCustomPost.published' => 'Y'),
  1119
+			'findType' => 'list',
  1120
+			'limit' => 2
  1121
+		);
  1122
+		$result = $Controller->paginate();
  1123
+		$expected = array(
  1124
+			1 => 'First Post',
  1125
+			2 => 'Second Post',
  1126
+		);
  1127
+		$this->assertEquals($expected, $result);
  1128
+		$result = $Controller->params['paging']['PaginatorCustomPost'];
  1129
+		$this->assertEquals(2, $result['current']);
  1130
+		$this->assertEquals(3, $result['count']);
  1131
+		$this->assertEquals(2, $result['pageCount']);
  1132
+		$this->assertTrue($result['nextPage']);
  1133
+		$this->assertFalse($result['prevPage']);
  1134
+	}
1081 1135
 
1082 1136
 /**
1083 1137
  * test paginate() and custom find with fields array, to make sure the correct count is returned.

0 notes on commit bce82a2

Please sign in to comment.
Something went wrong with that request. Please try again.