Skip to content

Commit

Permalink
fixed "no such component" error if rest component has not default nam…
Browse files Browse the repository at this point in the history
…e and was overridden in active record classes

added "useFilterKeyword" parameter to add possibility to use rest client with default query parameters as filters
  • Loading branch information
simialbi committed Apr 28, 2020
1 parent fbc6e1f commit 252b2f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
4 changes: 4 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 252b2f3

Please sign in to comment.