Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter model in a custom request #198

Open
Maykonn opened this issue Jul 29, 2020 · 2 comments
Open

Filter model in a custom request #198

Maykonn opened this issue Jul 29, 2020 · 2 comments

Comments

@Maykonn
Copy link

Maykonn commented Jul 29, 2020

@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.

@evan108108
Copy link
Owner

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...

@Maykonn
Copy link
Author

Maykonn commented Jul 30, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants