From 96e6ab30ebd4f406273aa3f7bdafd70f50ced77f Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 28 Dec 2017 14:00:07 +0530 Subject: [PATCH 1/2] Allow using custom finder with options. --- src/Traits/FindMethodTrait.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Traits/FindMethodTrait.php b/src/Traits/FindMethodTrait.php index 52817474f..7b5d265c3 100644 --- a/src/Traits/FindMethodTrait.php +++ b/src/Traits/FindMethodTrait.php @@ -12,8 +12,9 @@ trait FindMethodTrait * If `$method` is NULL the current value is returned * else the `findMethod` is changed * - * @param string|null $method Method name - * @return string + * @param string|array|null $method Method name as string or array where + * key is finder name and value is find options. + * @return string|array */ public function findMethod($method = null) { @@ -35,7 +36,13 @@ protected function _findRecord($id, Subject $subject) { $repository = $this->_table(); - $query = $repository->find($this->findMethod()); + $finder = $this->findMethod(); + $options = []; + if (is_array($finder)) { + $options = (array)current($finder); + $finder = key($finder); + } + $query = $repository->find($finder, $options); $query->where([current($query->aliasField($repository->primaryKey())) => $id]); $subject->set([ From 81a8bdd8688f5031a13335e63d893121f6b2280d Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 29 Dec 2017 12:57:51 +0530 Subject: [PATCH 2/2] Add tests for previous commit --- tests/App/Model/Table/BlogsTable.php | 16 ++++++++++++++++ tests/TestCase/Action/EditActionTest.php | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/App/Model/Table/BlogsTable.php b/tests/App/Model/Table/BlogsTable.php index 4ac267153..05b00e1fb 100644 --- a/tests/App/Model/Table/BlogsTable.php +++ b/tests/App/Model/Table/BlogsTable.php @@ -1,7 +1,23 @@ customOptions = $options; + return $query; + } } diff --git a/tests/TestCase/Action/EditActionTest.php b/tests/TestCase/Action/EditActionTest.php index 10cc2910c..a7673ac3e 100644 --- a/tests/TestCase/Action/EditActionTest.php +++ b/tests/TestCase/Action/EditActionTest.php @@ -74,6 +74,26 @@ public function testActionGetWithQueryArgs() $this->assertContains($expected, $result, '"body" do not match the expected value'); } + /** + * Test for custom finder with options. + * + * @return void + */ + public function testCustomFinder() + { + $this->_eventManager->on( + 'Dispatcher.invokeController', + ['priority' => 1000], + function ($event) { + $this->_controller->Crud->action('edit') + ->findMethod(['withCustomOptions' => ['foo' => 'bar']]); + } + ); + + $this->get('/blogs/edit/1'); + $this->assertSame(['foo' => 'bar'], $this->_controller->Blogs->customOptions); + } + /** * Test POST will update an existing record *