RESTFullYii icon indicating copy to clipboard operation
RESTFullYii copied to clipboard

Filter model in a custom request

Open Maykonn opened this issue 5 years ago • 2 comments

@evan108108 I have added a custom request like:

$this->onRest('req.get.customname.render', function() {
    // return the filtered data found (complex formating is not necessary in this example, just return the found results).
});

And calling it like:

/api/controllerName/customname?filter=[{"property": "slug", "value": "abc", "operator": "="},{"property": "relation.field", "value": "1234", "operator": "="}]

But it seems never triggering the model events or the model instantiating. I didn't found any example regarding how to use the events functionality when working with custom routes. What is the proper way to approach it?

Thanks in advance.

Maykonn avatar Jul 29 '20 21:07 Maykonn

Because you are creating a custom request the action has no idea what you are trying to do with your model thus you will have to create do it yourself. You can do it in the render or pre-render

$this->onRest('pre.filter.req.get.customname.render', function($data, $model_name, $relations, $count, $visibleProperties=[], $hiddenProperties=[]) {
	return [$data, $model_name, $relations, $count, $visibleProperties, $hiddenProperties]; //Array [Object, String, Array, Int, Array[String], Array[String]]
});

Then you have access in the render...

evan108108 avatar Jul 29 '20 23:07 evan108108

Once I added the code for pre.filter.req.get.customname.render I'm getting the following error:

Too few arguments to function ControllerNamerController::{closure}(), 2 passed in \/var\/www\/app\/protected\/vendor\/starship\/restfullyii\/starship\/RestfullYii\/events\/Eventor\/Eventor.php on line 170 and at least 4 expected 

It is a hard thing to understand. What are the necessary arguments and what function requires it? Being frank it seems impossible to identify what happened and how to fix it or even to know what params are expected for what function.

Maykonn avatar Jul 30 '20 13:07 Maykonn