Skip to content

Commit

Permalink
Attach query behavior in NgRestModel::find (luyadev#530)
Browse files Browse the repository at this point in the history
* Block Preview styling

* Tooltip preview fix luyadev#246

* Date plugin with custom format

* Changelog for luyadev#270

* Refactored namespace using

* Connection lost handling

* Unit test fixed close luyadev#229

* CONNECTION_ID only available in mysql

* Block Preview styling

* Tooltip preview fix luyadev#246

* travis tests with php 7.4

* clean up dist

* attache find behaviours

* Changelogs

* Unit tests

* phpdocs & changelog

* Update NgRestModel.php

* Update NgRestModel.php

* Update NgRestModel.php

* Update TestNgRestModel.php

Co-authored-by: Basil <git@nadar.io>
  • Loading branch information
2 people authored and slowfox089 committed Dec 10, 2020
1 parent 93aa325 commit 09bc56a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 3.3.3
## 3.4.0

### Added

+ [#530](https://github.com/luyadev/luya-module-admin/pull/530) Attach query behaviors in `luya\admin\ngrest\base\NgRestModel::find`.

### Fixed

+ [#529](https://github.com/luyadev/luya-module-admin/pull/529) Fixed an issue with OpenApi path params.
+ [#527](https://github.com/luyadev/luya-module-admin/issues/527) Fixed a bug where deleted user emails where not validated when save or update an existing user account.
Expand Down
56 changes: 55 additions & 1 deletion src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,68 @@ public function extraFields()
return array_merge(parent::extraFields(), $this->extractRootFields($extraFieldKeys));
}

/**
* Attach behaviors to the Active Query.
*
* Attach behaviours to every new {{\luya\admin\ngrest\base\NgRestActiveQuery}} on find() and ngRestFind().
* Returns a list of behaviors that the query component should behave as.
*
* As behavior methods can be access from the inner class, use full functions can be used inside the active query.
* It also enables the option to share standardized behaviors with functions (conditions), for example a soft delete condition.
*
* A behavior example
*
* ```php
* class MySuperBehavioir extends yii\base\Behavior
* {
* public function active($isActive = true)
* {
* return $this->andWhere(['is_active' => $isActive]);
* }
* }
* ```
*
* After attaching this behavior, it can be used like `MyModel::find()->active()->one()`.
*
* > Whenever possible, directly create a custom Active Query, as it provices full IDE support. The behavior
* > does not, the example above will even show an IDE error because the mmethod `andWhere()` does not exsist
* > in the yii\base\Behavior class.
*
* The return value of this method should be an array of behavior objects or configurations
* indexed by behavior names. A behavior configuration can be either a string specifying
* the behavior class or an array of the following structure:
*
* ```php
* 'behaviorName' => [
* 'class' => 'BehaviorClass',
* 'property1' => 'value1',
* 'property2' => 'value2',
* ]
* ```
* @see {{\yii\base\Component::behaviors}}
*
* @return array
* @since 3.4.0
*/
public static function findActiveQueryBehaviors()
{
return [];
}

/**
* {@inheritdoc}
*
* @return NgRestActiveQuery
*/
public static function find()
{
return new NgRestActiveQuery(get_called_class());
$config = [];

foreach (static::findActiveQueryBehaviors() as $name => $class) {
$config['as ' . $name] = $class;
}

return new NgRestActiveQuery(get_called_class(), $config);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/admin/ngrest/base/NgRestModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function testBehaviorIsAttached()
$this->assertArrayHasKey('NgRestEventBehavior', $behaviors);
$this->assertArrayHasKey('LogBehavior', $behaviors);
}

public function testQueryBehaviorsAreAttached()
{
$query = TestNgRestModel::find();
$behaviors = $query->behaviors;

$this->assertArrayHasKey('DummyBehavior', $behaviors);
}

public function testGenericSearchFields()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/data/models/TestNgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace admintests\data\models;

use admintests\data\stubs\StubBehavior;
use luya\admin\ngrest\base\NgRestModel;
use luya\admin\aws\TaggableActiveWindow;

Expand All @@ -22,6 +23,13 @@ public static function ngRestApiEndpoint()
return 'foo-bar';
}

public static function findActiveQueryBehaviors()
{
return [
'DummyBehavior' => StubBehavior::class
];
}

public function rules()
{
return [
Expand Down
10 changes: 10 additions & 0 deletions tests/data/stubs/StubBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace admintests\data\stubs;

use yii\base\Behavior;

class StubBehavior extends Behavior
{

}

0 comments on commit 09bc56a

Please sign in to comment.