S
S
Sokolov092015-06-11 18:54:20
Yii
Sokolov09, 2015-06-11 18:54:20

How to form a query with a filter in rest yii?

There is a controller Product
namespace app\controllers;
use yii\rest\ActiveController;
class ProductController extends ActiveController
{
public $modelClass = 'app\models\Product';
}
GET /products getting goods
GET /product/{id} getting goods
How to create a query: list of user's goods?
In urlmanager I created a rule to process my request which will call my action.
GET /products/user/{id} - passes but I don't know how to implement the action correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
matperez, 2015-06-11
@Sokolov09

yii\rest\IndexAction has a 'prepareDataProvider' method. If overridden, you can use the get parameters in the query to make a fetch. For example like this:

public function actions()
    {
        return [
            'index' => [
                'class' => IndexAction::className(),
                'modelClass' => $this->modelClass,
                'checkAccess' => [$this, 'checkAccess'],
                'prepareDataProvider' => function ($action) {
                    $model = new Product();
                    $model->load(\Yii::$app->request->queryParams)
                    return $model->search();
                }
            ],
        ];
    }

model->search should return an ActiveDataProvider based on the passed parameters from the request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question