Skip to content

Commit 252b2f3

Browse files
author
simialbi
committed
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
1 parent fbc6e1f commit 252b2f3

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/ActiveRecord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static function getDb()
9999
public static function modelName()
100100
{
101101
$path = Inflector::camel2id(StringHelper::basename(get_called_class()), '-');
102-
return self::getDb()->usePluralisation ? Inflector::pluralize($path) : $path;
102+
return static::getDb()->usePluralisation ? Inflector::pluralize($path) : $path;
103103
}
104104

105105

src/Command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ protected function queryInternal($method = 'get')
170170
if ($this->db->usePluralisation && strpos($this->pathInfo, '/') === false) {
171171
$this->pathInfo = Inflector::pluralize($this->pathInfo);
172172
}
173+
if (!$this->db->useFilterKeyword) {
174+
$filter = ArrayHelper::remove($this->queryParams, 'filter', []);
175+
$this->queryParams = array_merge($this->queryParams, $filter);
176+
}
173177

174178
return $this->db->$method($this->pathInfo, $this->queryParams);
175179
}

src/Connection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ class Connection extends Component
5757
*/
5858
public $responseConfig = [];
5959
/**
60-
* @var boolean Whether to user pluralisation or not
60+
* @var boolean Whether to use pluralisation or not
6161
*/
6262
public $usePluralisation = true;
63+
/**
64+
* @var boolean Whether to use filter keyword or not
65+
*/
66+
public $useFilterKeyword = true;
6367
/**
6468
* @var boolean Whether the connection should throw an exception if response is not 200 or not
6569
*/

tests/UrlTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ public function testGetAnotherOne()
4040
$this->assertStringStartsWith('https://api.site.com/rest-models/1', $logEntry['url']);
4141
}
4242

43+
public function testFilter()
44+
{
45+
RestModel::find()->where(['name' => 'John'])->one();
46+
47+
$logEntry = $this->parseLogs();
48+
49+
$this->assertEquals('GET', $logEntry['method']);
50+
$this->assertStringStartsWith('https://api.site.com/rest-models?filter%5Bname%5D=John', $logEntry['url']);
51+
}
52+
53+
public function testWithoutFilterKeyword()
54+
{
55+
$this->mockWebApplication([
56+
'components' => [
57+
'rest' => [
58+
'useFilterKeyword' => false
59+
]
60+
]
61+
]);
62+
63+
RestModel::find()->where(['name' => 'John'])->one();
64+
65+
$logEntry = $this->parseLogs();
66+
67+
$this->assertEquals('GET', $logEntry['method']);
68+
$this->assertStringStartsWith('https://api.site.com/rest-models?name=John', $logEntry['url']);
69+
}
70+
4371
public function testDeleteOne()
4472
{
4573
$model = new RestModel([

0 commit comments

Comments
 (0)