Browse Source

Add finder options to Paginator->paginate method
The paginator right now doesn't send $options to any finder, custom or otherwise. This change allows setting 'finderOptions' directly from the controller's paginate array.

Patrick Conroy 11 years ago
parent
commit
56d6f04136

+ 1 - 1
src/Controller/Component/PaginatorComponent.php

@@ -164,7 +164,7 @@ class PaginatorComponent extends Component {
 		unset($options['finder'], $options['maxLimit']);
 
 		if (empty($query)) {
-			$query = $object->find($type);
+			$query = $object->find($type, $options);
 		}
 
 		$query->applyOptions($options);

+ 21 - 0
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -142,6 +142,27 @@ class PaginatorComponentTest extends TestCase {
 	}
 
 /**
+ * Test to make sure options get sent to custom finder methods via paginate
+ * @return void
+ */
+	public function testPaginateCustomFinderOptions() {
+		$this->loadFixtures('Post');
+		$settings = [
+			'PaginatorPosts' => [
+				'finder' => 'author',
+				'author_id' => 1
+			]
+		];
+		$table = TableRegistry::get('PaginatorPosts');
+
+		$expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
+			->count();
+		$result = $this->Paginator->paginate($table, $settings)->count();
+
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  *
  * @return void

+ 14 - 0
tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php

@@ -52,4 +52,18 @@ class PaginatorPostsTable extends Table {
 		return $query;
 	}
 
+/**
+ * Custom finder, used with fixture data to ensure Paginator is sending options
+ *
+ * @param Cake\ORM\Query $query
+ * @param array $options
+ * @return Cake\ORM\Query
+ */
+	public function findAuthor(Query $query, array $options = []) {
+		if (isset($options['author_id'])) {
+			$query->where(['PaginatorPosts.author_id' => $options['author_id']]);
+		}
+		return $query;
+	}
+
 }