diff --git a/CHANGELOG.md b/CHANGELOG.md index 84de16902e..de2e596296 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/ngrest/base/NgRestModel.php b/src/ngrest/base/NgRestModel.php index 7c20825067..255128207e 100644 --- a/src/ngrest/base/NgRestModel.php +++ b/src/ngrest/base/NgRestModel.php @@ -109,6 +109,54 @@ 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} * @@ -116,7 +164,13 @@ public function extraFields() */ 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); } /** diff --git a/tests/admin/ngrest/base/NgRestModelTest.php b/tests/admin/ngrest/base/NgRestModelTest.php index e76dd4d929..380c7be7d1 100644 --- a/tests/admin/ngrest/base/NgRestModelTest.php +++ b/tests/admin/ngrest/base/NgRestModelTest.php @@ -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() { diff --git a/tests/data/models/TestNgRestModel.php b/tests/data/models/TestNgRestModel.php index eb005a9897..dbf8d634fc 100644 --- a/tests/data/models/TestNgRestModel.php +++ b/tests/data/models/TestNgRestModel.php @@ -2,6 +2,7 @@ namespace admintests\data\models; +use admintests\data\stubs\StubBehavior; use luya\admin\ngrest\base\NgRestModel; use luya\admin\aws\TaggableActiveWindow; @@ -22,6 +23,13 @@ public static function ngRestApiEndpoint() return 'foo-bar'; } + public static function findActiveQueryBehaviors() + { + return [ + 'DummyBehavior' => StubBehavior::class + ]; + } + public function rules() { return [ diff --git a/tests/data/stubs/StubBehavior.php b/tests/data/stubs/StubBehavior.php new file mode 100644 index 0000000000..3b1cdf4112 --- /dev/null +++ b/tests/data/stubs/StubBehavior.php @@ -0,0 +1,10 @@ +