From 252b2f31fdc670b4ef6bd504f743ddbb30f30b5f Mon Sep 17 00:00:00 2001 From: simialbi Date: Tue, 28 Apr 2020 15:02:53 +0200 Subject: [PATCH] fixed "no such component" error if rest component has not default name and was overridden in active record classes added "useFilterKeyword" parameter to add possibility to use rest client with default query parameters as filters --- src/ActiveRecord.php | 2 +- src/Command.php | 4 ++++ src/Connection.php | 6 +++++- tests/UrlTest.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index b722b05..41a18a7 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -99,7 +99,7 @@ public static function getDb() public static function modelName() { $path = Inflector::camel2id(StringHelper::basename(get_called_class()), '-'); - return self::getDb()->usePluralisation ? Inflector::pluralize($path) : $path; + return static::getDb()->usePluralisation ? Inflector::pluralize($path) : $path; } diff --git a/src/Command.php b/src/Command.php index eebf68b..b672a02 100644 --- a/src/Command.php +++ b/src/Command.php @@ -170,6 +170,10 @@ protected function queryInternal($method = 'get') if ($this->db->usePluralisation && strpos($this->pathInfo, '/') === false) { $this->pathInfo = Inflector::pluralize($this->pathInfo); } + if (!$this->db->useFilterKeyword) { + $filter = ArrayHelper::remove($this->queryParams, 'filter', []); + $this->queryParams = array_merge($this->queryParams, $filter); + } return $this->db->$method($this->pathInfo, $this->queryParams); } diff --git a/src/Connection.php b/src/Connection.php index 9aea8dd..3d314f0 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -57,9 +57,13 @@ class Connection extends Component */ public $responseConfig = []; /** - * @var boolean Whether to user pluralisation or not + * @var boolean Whether to use pluralisation or not */ public $usePluralisation = true; + /** + * @var boolean Whether to use filter keyword or not + */ + public $useFilterKeyword = true; /** * @var boolean Whether the connection should throw an exception if response is not 200 or not */ diff --git a/tests/UrlTest.php b/tests/UrlTest.php index 851d797..7ca5160 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -40,6 +40,34 @@ public function testGetAnotherOne() $this->assertStringStartsWith('https://api.site.com/rest-models/1', $logEntry['url']); } + public function testFilter() + { + RestModel::find()->where(['name' => 'John'])->one(); + + $logEntry = $this->parseLogs(); + + $this->assertEquals('GET', $logEntry['method']); + $this->assertStringStartsWith('https://api.site.com/rest-models?filter%5Bname%5D=John', $logEntry['url']); + } + + public function testWithoutFilterKeyword() + { + $this->mockWebApplication([ + 'components' => [ + 'rest' => [ + 'useFilterKeyword' => false + ] + ] + ]); + + RestModel::find()->where(['name' => 'John'])->one(); + + $logEntry = $this->parseLogs(); + + $this->assertEquals('GET', $logEntry['method']); + $this->assertStringStartsWith('https://api.site.com/rest-models?name=John', $logEntry['url']); + } + public function testDeleteOne() { $model = new RestModel([